Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
990 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...