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?
The Html. Editor() or Html. EditorFor() extension methods generate HTML elements based on the data type of the model object's property.
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.
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.
EditorFor(model => model. userName, new { htmlAttributes = new { disabled = "disabled", @readonly = "readonly" } }) .
ViewData is a dictionary.
You can write ViewData["Title"]
, or you can loop through ViewData
(which is a collection of KeyValuePairs) or ViewData.Keys
.
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"])%>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With