0

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"})

2 Answers 2

1

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().

Sign up to request clarification or add additional context in comments.

Comments

0

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

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

1 Comment

I was thinking of doing that but I wanted a cleaner approach. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.