1

I pass some dummy data to my View just to have something to work with :

public ActionResult Index()
        {
            ViewBag.Message = "Create table";
            var model = new List<Auction>();
            model.Add(new Auction
            {
                Title = "First Title",
                Description = "First Description"
            });
            model.Add(new Auction
            {
                Title = "Second Title",
                Description = "Second Description"
            });
            model.Add(new Auction
            {
                Title = "Third Title",
                Description = "Third Description"
            });
            model.Add(new Auction
            {
                Title = "Fourht Title",
                Description = "Fourth Description"
            });

            return View(model);
        }

I display this in my view :

@model List<Ebuy.Website.Models.Auction>

@{
    ViewBag.Title = "Home Page";
}


@using (Html.BeginForm())
{
    <table border="1" >
        @for (var i = 0; i < Model.Count(); i++)
        {
            <tr>

                <td>
                    @Html.HiddenFor(x => x[i].Id)
                    @Html.DisplayFor(x => x[i].Title)
                </td>
                <td>
                    @Html.EditorFor(x => x[i].Description)
                </td>
            </tr>
        }
    </table>

    <button type="submit">Save</button>
}

and now I want to make some manipulation of the submitted data :

 [HttpPost]
        public ActionResult Index(List<Auction> model)
        {
            var test = model;
            model[1].Title = "Test";
            return View(model);
        }

But when I debug this I see that the Description properties are send back but the Title properties don't have value. As you can see above I display them with @Html.DisplayFor(x => x[i].Title). Is this some default behaviour or am I doing something wrong?

1 Answer 1

6

@Html.DisplayFor doesn't output an input field (or any form field for that matter) so it isn't part of the values the form posts.

If you want the title posted as part of the form you may want to add an additional

@Html.HiddenFor(x => x[i].Title)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Maybe there is really no need to keep track of the read only values. Gonna rethink what I want. But good to know how @Html.DisplayFor works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.