Search code examples
c#asp.net-core-mvcmodel-binding

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


I have a class which its name is "Question.cs" and I have another one that its name is "Answer.cs". I want to send List of Questions to my action instead of one Question and I don't know how can I do it. In fact, I don't know how to get the Questions as parameter. Can anyone tell me how I can receive the Questions in the action? Here are my 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; } 
}

This is my Razor View (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>

As you see, I used a foreach loop which is give me all of the "Questions". So I can't receive just one "Question" in my action. These are my actions:

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());
}

I think I have to get List<Question> in my second action but I don't know how? Can anyone help me step by step?


Solution

  • Please refer to the following steps to modify your code:

    1. In the View page, use for loop to loop through the Model and display the value. code like this:

       @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. In the Post method, change the parameter data type to 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());
       }
      

    Then, the result like this:

    enter image description here