I have an object model and I am passing it to the view so that user can input their comments for some of the object's properties as a part of a survey.
I am able to receive what the user has entered for an object if I am only rendered one single object to the view. However, when I want to render multiple (a list) of objects to the view then I receive a null list of objects when the user click on the submit form.
Please see my code below:
This is my object model
public class SurveyViewModel
{
public string Name { get; set; }
public double PV { get; set; }
public double QtyUsePerMonth { get; set; }
public double TotalPVPerMonth { get; set; }
}
This is my view where I render the list of object
@model IEnumerable<WebApplication4.Models.SurveyViewModel>
@{
ViewBag.Title = "Survey";
}
<h2>Survey</h2>
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "form1" }))
{
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.PV)
</th>
<th>
@Html.DisplayNameFor(model => model.QtyUsePerMonth)
</th>
<th>
@Html.DisplayNameFor(model => model.TotalPVPerMonth)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
@Html.HiddenFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.PV)
@Html.HiddenFor(modelItem => item.PV)
</td>
<td>
@Html.TextBoxFor(modelItem => item.QtyUsePerMonth)
</td>
<td>
@Html.TextBoxFor(modelItem => item.TotalPVPerMonth)
</td>
</tr>
}
</table>
<input type="submit" value="Submit">
}
And this is my HttpPost method
[HttpPost]
public ActionResult Survey(List<Models.SurveyViewModel> model)
{
...
}
When the user click on the Submit button I got a null for model where I am expecting to see a list.
Please let me know what I am doing wrong.
Thank you for your help.