Shuaib
Shuaib

Reputation: 1579

Add HTML parameter to MVC3 @Html.EditorFor(model => model.Name)

How comethis does not work? I am trying to add a new HTML attribute to the EditorFor. @Html.EditorFor(model => model.Name, new { data_autocomplete="/index/QuickSearch"})

Upvotes: 0

Views: 842

Answers (2)

Keith
Keith

Reputation: 5381

Unfortunately the EditorFor helper doesn't include the html attributes parameter that the other helpers do. This is because you can make custom editor templates (see http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-1-introduction.html for an outdated MVC2 example), so it doesn't make sense to have html attributes, as there could be many HTML tags in the template.

Your best bet is to either write your own helper extension method OR, if your editor is always, for example, a TextBox, use TextBoxFor().

Upvotes: 1

BumbleB2na
BumbleB2na

Reputation: 10743

with jQuery you could attach the attribute after the DOM has finished loading:

document.ready(function() {
    $('#Name').attr('data-autocomplete', '/index/QuickSearch');
}

Upvotes: 0

Related Questions