@labelfor and@displayfor values becomes null in post RRS feed

  • Question

  • User-698357872 posted

    I have a view which contains more than 75 fields, in which am binding the model using editorfor and labelfor. But when the form post occurs the labelfor values becomes null. since label is a read only field its value becomes null. how can we handle this scenario without hiddenfield.

    Kindly suggest

    Wednesday, September 23, 2020 3:02 PM

All replies

  • User-474980206 posted

    to round trip the data you need to use a hidden field. a better approach would be to store the value on the server, and refetch on post back. otherwise you should encrypt the hidden field data to prevent tampering.

    Wednesday, September 23, 2020 3:11 PM
  • User-698357872 posted

    thank you bruce

    currently am using tempdata to store the value when am binding the model to view and reassign the values to model (only the label values) using reflection concept

    is it right approach to do

    Wednesday, September 23, 2020 3:37 PM
  • User1686398519 posted

    Hi Arunmagesh, 

    1. TempData can be used to store data, but it is a very short-lived instance and should only be used in the current request and subsequent requests.
    2. If you want to perform model binding, I suggest you use HiddenFor, which is better.
      1. As long as the name of the form control on the view is the same as the field name in the complex type, it will automatically convert the input field data on the view into the properties of a complex type parameter of an action method in HttpPost request.
      2. DisplayFor does not generate any HTML markup, it only outputs formatted text.
      3. In other words, using DisplayFor will not generate a label with a name, so when the model is bound after the form is submitted, the data is empty.
    3. I wrote a simple example to help you understand, please refer to it. .


        public class TestModel
            public int Id { get; set; }
            public string Name { get; set; }


        public class Test456Controller : Controller
            public ActionResult Index()
                TestModel test = new TestModel { Id = 1, Name = "1" };
                return View(test);
            public ActionResult Index(TestModel test)
                return View();


    @model DailyMVCDemo.Models.TestModel
    @using (Html.BeginForm())
        @Html.HiddenFor(model => model.Id)
        @Html.LabelFor(model => model.Id)
        @Html.DisplayFor(model => model.Id)<br />
        @Html.LabelFor(model => model.Name)
        @Html.TextBoxFor(model => model.Name)
        <button type="submit">submit</button>

    Here is the result.

    Best Regards,


    Thursday, September 24, 2020 8:50 AM