Nate Pet
Nate Pet

Reputation: 46322

MVC @Html.Display()

I have something like:

 <input type="text" name="TerrMng" id="TerrMng"/>  

in HTML. What is the equivalent of the above using @Html.Display?

I tried using: @Html.Display("TerrMng", TerrMng)

but was not successful. Note that I like to use @Html.Display but not sure how to translate the ID value so that it shows up.

Upvotes: 7

Views: 59664

Answers (4)

Mladen B.
Mladen B.

Reputation: 3025

@Html.Display() is used instead of @Html.DisplayFor() when your model is not known at compile time, or if you prefer to work with strings, rather than with strong types. For example, these 2 are equivalents (given that your model is some class):

@Html.DisplayFor(m => m.MyProperty)

and

@Html.Display("MyProperty")

But the additional cool feature of the Display() method is that it can also do the lookup in the ViewData, and not just in your Model class. For example, here is a way to display the HTML for the property on a random object, given that we know it has a property named "Blah" (the type of the object doesn't really matter):

@{ ViewData["itsawonderfullife"] = SomeObject; }
<div>@Html.Display("itsawonderfullife.Blah")</div>

This way, we are telling HtmlHelper to look into the ViewData, instead of our Model, and to display the property Blah of a given SomeObject.

Upvotes: 1

Josh Mein
Josh Mein

Reputation: 28665

This should do the trick if you are just wanting to display the data and not allow the user to edit the information.

@Html.DisplayFor(m => m.TerrMng);

Edit:

what-is-the-html-displayfor-syntax-for is another question on stackoverflow that may give you some more guidance.

Edit:

TerrMng does not exist on PageLoad so you cannot use the Html.Display in that way. You need to create it and fill its value with the value received from the jQuery. In this case where you would have to do the following:

HTML

 @Html.Display("TerrMng"); // This creates the label with an id of TerrMng

jQuery

 $("#TerrMng").val(TerrMng); // This puts the value of the javascript variable into the label

Upvotes: 5

Patrick Karcher
Patrick Karcher

Reputation: 23613

The Display method is not for creating input boxes. You'd want to use:

@Html.TextBoxFor(m => m.TerrMng);

or the templated helper method:

@Html.EditorFor(m => m.TerrMng);

I'm assuming that you want to use modelbinding. If not, if you really just want to use a helper to simply make an input tag, use:

@Html.TextBox("TerrMng");

This would be sent to the client:

<input id="TerrMng" type="text" value="" name="TerrMng">

The first 2 methods above would result in the exact same html, if model.TerrMng was "" or String.Empty. If for some reason you don't want the value attribute, you'll need to type it out yourself.

Upvotes: 5

Hari Gillala
Hari Gillala

Reputation: 11926

You could try something based on this. This is not exact but you could get some idea.

@Html.TextBoxFor(yourmodel => model.yourModelFieldname, null)

Upvotes: 3

Related Questions