Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html.EditorFor additionalViewData

I have a custom editor template where I add values to the ViewData like so:

@Html.EditorFor( model => model.PhoneNumber , new { Title = "SomeValue" } )

How can I access both the value and the property name?

like image 924
Boo Avatar asked Dec 16 '10 03:12

Boo


People also ask

What is use of HTML EditorFor?

The Html. Editor() or Html. EditorFor() extension methods generate HTML elements based on the data type of the model object's property.

How do I add an EditorFor style?

EditorFor does not allow for styling as there are no parameters for additional attributes. The reason for this is because the EditorFor doesn't always generate a single element as it can be overridden. To style a specific type of element you need to use the specific editor you want to use.

What is difference between EditorFor and TextBoxFor in MVC?

Show activity on this post. TextBoxFor: It will render like text input html element corresponding to specified expression. In simple word it will always render like an input textbox irrespective datatype of the property which is getting bind with the control. EditorFor: This control is bit smart.

How do you make an editor read only in MVC?

EditorFor(model => model. userName, new { htmlAttributes = new { disabled = "disabled", @readonly = "readonly" } }) .


2 Answers

ViewData is a dictionary.

You can write ViewData["Title"], or you can loop through ViewData (which is a collection of KeyValuePairs) or ViewData.Keys.

like image 156
SLaks Avatar answered Oct 16 '22 10:10

SLaks


You can nest your htmlAttributes object in view data:

<%= Html.EditorFor(model => model.PhoneNumber, new { htmlAttributes = new { Title = "SomeValue" } })

Then in your editor template:

<%= Html.TextBox("", Model.Value, ViewData["htmlAttributes"])%>
like image 38
FRj Avatar answered Oct 16 '22 09:10

FRj