Boo
Boo

Reputation: 1644

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?

Upvotes: 57

Views: 36608

Answers (2)

FRj
FRj

Reputation: 341

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

Upvotes: 34

SLaks
SLaks

Reputation: 887275

ViewData is a dictionary.

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

Upvotes: 63

Related Questions