Reputation: 210
I am trying to return the results of a table back to the controller for further manipulation. Once returned to the controller the value shows as null. In the past I have been able to use @Html.HiddenFor to return the values but it doesn't seem to be working in this instance. Not sure what I am doing wrong here. Any help is greatly appreciated.
@model IEnumerable<Project.Models.Item>
@{
ViewBag.Title = "Welcome to The Project";
}
@using (Html.BeginForm("UpdateQuality", "Home", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
<div class="row">
<div class="form-group">
<table class="table table-bordered">
<tr>
<th>@Html.DisplayNameFor(m => m.Name)</th>
<th>@Html.DisplayNameFor(m => m.SellIn)</th>
<th>@Html.DisplayNameFor(m => m.Quality)</th>
</tr>
@for (int i = 0; i < Model.Count(); i++)
{
<tr>
<td>@Html.DisplayFor(m => m.ElementAt(i).Name)</td>
<td>@Html.DisplayFor(m => m.ElementAt(i).SellIn)</td>
<td>@Html.DisplayFor(m => m.ElementAt(i).Quality)</td>
@Html.HiddenFor(m => m.ElementAt(i).Name)
@Html.HiddenFor(m => m.ElementAt(i).SellIn)
@Html.HiddenFor(m => m.ElementAt(i).Quality)
</tr>
}
</table>
<div class="form-group">
<div style="margin-top: 50px">
<input type="submit" class="btn btn-primary" value="Advance Day"/>
</div>
</div>
</div>
</div>
}
And here is the controller which returns null.
public ActionResult UpdateQuality(List<Item> Items )
{
return View("Index", (object)Items);
}
Upvotes: 0
Views: 1998
Reputation:
You cannot use ElementAt()
in a HtmlHelper
method that generates form controls (look at the name
attribute your generating - it does not match your model).
Either change the model to be IList<T>
@model List<Project.Models.Item>
and use a for
loop
@for (int i = 0; i < Model.Count; i++)
{
....
@Html.HiddenFor(m => m.[i].Name)
....
or change use a custom EditorTemplate
for typeof Item
, and in the main view, use @Html.EditorFor(m => m)
to generate the correct html for each item in the collection.
Upvotes: 3