7

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.

4 Answers 4

5

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
Sign up to request clarification or add additional context in comments.

3 Comments

Whats the error? Are you using model binding and is TerrMng actually a part of that model?
TerrMng is just a value that I get from jquery.
@Nate Pet That is why it isnt working. I will update my answer.
5

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.

1 Comment

@Patrick Karcher - Patrick, I like to have it as a Display so the user cannot modify it. Why doesn't something like @Html.Display("TerrMng") work as it does not show me anything but @Html.TextBox("TerrMng") works
3

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

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

Comments

1

@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.

Comments

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.