3

In Web Forms, a Label displays text which persists between postbacks. What is the equivalent in MVC4?

I don't mean Html.LabelFor because that doesn't persist the model property it is bound to on postback. At the moment, I am doing:

@Model.Property
@Html.HiddenFor(o => o.Property)

Is there a better way?

James
  • 7,343
  • 9
  • 46
  • 82

1 Answers1

4

What is the equivalent in MVC4?

@Html.LabelFor(o => o.Property)

will create a <label> HTML element referring to the corresponding <input> element.

Or:

@Html.DisplayFor(o => o.Property)

which will simply output the value of the property as plain text.

or if you want to persist the value on postback you could use either a hidden field (as you already do) or an editor template:

@Html.EditorFor(o => o.Property)

And if you want to have the value of the label displayed on the screen and sent to the server you could use a conjunction of a DisplayFor and HiddenFor elements:

@Html.DisplayFor(o => o.Property)
@Html.HiddenFor(o => o.Property)
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 2
    Then you use a hidden field or an editor template which will render a visible input field. If you want the value to be persisted on postback this value needs to be stored as input field inside the form that you are posting. Oh, and remember that in ASP.NET MVC there's no longer such notion as *Postback*. An HTML `
    ` could be submitted to any controller action, not necessarily the one that rendered it. It's a completely different way of doing web development.
    – Darin Dimitrov Jan 05 '13 at 10:32
  • Thanks. What's the difference between `@Property` and `@Html.DisplayFor(o => o.Property)`? – James Jan 05 '13 at 10:38
  • The difference is that `@Property` will simply display the value of the property as-is, whereas `@Html.DisplayFor(o => o.Property)` will render the corresponding display template. You can read more about editor/display templates here: http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-3-default-templates.html – Darin Dimitrov Jan 05 '13 at 10:39
  • Basically `@Html.DisplayFor(o => o.Property)` will analyze the type of the property. Let's suppose that it is a collection of some other type: `IEnumerable`. Then the helper will automatically look for a display template situated in `~/Views/Shared/DisplayTemplates/FooBar.cshtml` and will automatically render this template **for each** element of the collection. The templating system is extremely powerful in ASP.NET MVC because it allows you to stop writing ugly foreach loops in your views. It all works by conventions. – Darin Dimitrov Jan 05 '13 at 10:40
  • And how do I set the display template for a property? (I.e. it would be helpful for me to build one which automatically displays the text and stores it in a hidden field.) – James Jan 05 '13 at 10:40
  • 1
    The templating system works by convention. For example if the type of the property is string, then it will look for `~/Views/Shared/DisplayTemplates/string.cshtml`. But you could always override it with a custom name: `@Html.DisplayFor(o => o.Property, "MyTemplate")`. Now this will render `~/Views/Shared/DisplayTemplates/MyTemplate.cshtml`. I very strongly invite you to read the article I've linked to in my previous comment to better familiarize yourself with the templating system. – Darin Dimitrov Jan 05 '13 at 10:42