如何在 ASP.NET Core MVC 中获取 List<object> 作为参数

How to get List<object> as parameter in action in ASP.NET Core MVC

我有一个 class,它的名字是“Question.cs”,我还有另一个,它的名字是“Answer.cs”。 我想将问题列表而不是一个问题发送到我的操作中,但我不知道该怎么做。 事实上,我不知道如何将问题作为参数。谁能告诉我如何在行动中收到问题? 这是我的 classes:

public class Question
{
    [Key]
    public int QuestionId { get; set; }

    [Required]
    [MaxLength(500)]
    public string QuestionTitle { get; set; }
    
    [Required]
    [MaxLength(500)]
    public string QuestionAnswer { get; set; }

    //property
    public List<Answer> Answers { get; set; }
}
public class Answer
{
    [Key]
    public int AnswerId { get; set; }
    [Required]
    public int QuestionId { get; set; }
    [Required]
    [MaxLength(500)]
    public string AnswerTitle { get; set; }
    
    //property
    [ForeignKey("QuestionId")]
    public Question Question { get; set; } 
}

这是我的 Razor 视图 (Question.cshtml):

@using GameShop.Data.Domain.QuestIonsAnswers
@model List<GameShop.Data.Domain.QuestIonsAnswers.Question>
@{
    Layout = "~/Areas/Admin/Views/Shared/_AdminLayout.cshtml";
}
<form asp-action="Index" method="POST">
    @foreach (var item in Model)
    {
        <div>@item.QuestionTitle</div>
        <input asp-for="@item.QuestionAnswer" Value="@item.QuestionId" />
        <br />
    }
    <input type="submit" class="btn btn-primary" value="ثبت">
</form>

如您所见,我使用了一个 foreach 循环,它为我提供了所有“问题”。所以我不能在我的行动中只收到一个“问题”。这些是我的行为:

public IActionResult Index()
{
    return View(_context.Questions.ToList());
}

[HttpPost]
public IActionResult Index(Question question)
{
    foreach (var item in question)
    {
        var ans=new Answer(){
            QuestionId=item.QuestionId,
            AnswerTitle=item.QuestionAnswer
        };
        _context.Answers.Add(ans);
        _context.SaveChanges();
    }
    return View(_context.Questions.ToList());
}

我想我必须在第二个行动中获得 List<Question>,但我不知道怎么做? 任何人都可以一步一步地帮助我吗?

请参考以下步骤修改您的代码:

  1. 在View页面中,使用for循环遍历Model并显示值。代码如下:

     @model List<WebApplication6.Models.Question>
    
     @{
         ViewData["Title"] = "Index";
         Layout = "~/Views/Shared/_Layout.cshtml";
     } 
     <h1>Index</h1> 
     <form asp-action="Index" method="POST">
             @for (var i = 0; i < Model.Count(); i++)
             {
                 <div>@Model[i].QuestionTitle</div>
                 <input asp-for="@Model[i].QuestionId" type="hidden" />
                 <input asp-for="@Model[i].QuestionTitle" type="hidden" />
                 <input asp-for="@Model[i].QuestionAnswer" />
                 <br />
             } 
         <input type="submit" class="btn btn-primary" value="ثبت">
     </form> 
    
  2. 在Post方法中,将参数数据类型改为List<Question>

     [HttpPost]
     public IActionResult Index(List<Question> questions)
     {
         foreach (var item in questions)
         {
             //var ans = new Answer()
             //{
             //    QuestionId = item.QuestionId,
             //    AnswerTitle = item.QuestionAnswer
             //};
             //_context.Answers.Add(ans);
             //_context.SaveChanges();
         }
         return View(_context.Questions.ToList());
     }
    

那么,结果是这样的: