Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
989 views
in Technique[技术] by (71.8m points)

asp.net mvc - @Html.HiddenFor returning null value

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);
}   
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...