0

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);
}   
snapper
  • 210
  • 3
  • 14

1 Answers1

3

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.

  • Hey Stephen this is working correctly now. It sends my table data to the controller where some simple math is performed and then returns the updated data back to the view and it is displayed correctly on the table. However if I click submit again it returns the original table data and not the updated data. Am I missing something? Let me know if you need more info. – snapper Jul 29 '17 at 19:30
  • If you're using a custom template, I'm a bit confused about how you'd iterate over the collection - m => m just submits the parent object to the template? – Bob Tway Dec 08 '17 at 10:51
  • @MattThrower. In your case [based on you previous question](https://stackoverflow.com/questions/47712269/net-mvc-post-back-of-child-collection-is-always-empty) you would have a `/Views/Shared/EditorTemplates/Tnc.cshtml` view with `@model Tnc` and `@Html.HiddenFor(m => m.ListTncId)` etc, then in the main view you would use `@Html.EditorFor(m => m.Tncs)` to generate the html for each item in the collection –  Dec 08 '17 at 10:55
  • @MattThrower. [this answer](https://stackoverflow.com/questions/30094047/post-an-html-table-to-ado-net-datatable/30094943#30094943) might help to understand it better –  Dec 08 '17 at 10:56
  • Thank you. Got it working now - you have been exceptionally helpful, and I appreciate your time & patience. – Bob Tway Dec 08 '17 at 11:01