Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 301
Nate Pet Avatar asked Nov 01 '11 15:11

Nate Pet


People also ask

What is display for in MVC?

Display modes in ASP.NET MVC 5 provide a way of separating page content from the way it is rendered on various devices, like web, mobile, iPhone, iPod and Windows Phones. All you need to do is to define a display mode for each device, or class of devices.

How add HTML to MVC?

There are two ways in MVC to create custom Html helpers as below. We can create our own HTML helper by writing extension method for HTML helper class. These helpers are available to Helper property of class and you can use then just like inbuilt helpers. Add new class in MVC application and give it meaningful name.

Does MVC use HTML?

It is not mandatory to use HTML Helper classes for building an ASP.NET MVC application. We can build an ASP.NET MVC application without using them, but HTML Helpers helps in the rapid development of a view.


2 Answers

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.

like image 118
Patrick Karcher Avatar answered Oct 17 '22 01:10

Patrick Karcher


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
like image 30
Josh Mein Avatar answered Oct 17 '22 00:10

Josh Mein