Html.EditorFor additionalViewData

.Netasp.net Mvc-3

.Net Problem Overview


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?

.Net Solutions


Solution 1 - .Net

ViewData is a dictionary.

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

Solution 2 - .Net

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"])%>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBooView Question on Stackoverflow
Solution 1 - .NetSLaksView Answer on Stackoverflow
Solution 2 - .NetFRjView Answer on Stackoverflow