I would like to autofocus on an editorfor in my application, but I can\'t seem to do that. I have successfully used autofocus on a textbox, but I would like to use an editor
This s because you are using EditorFor instead of something specific like TextBoxFor.
@Html.TextBoxFor(model => model.Name, new { htmlAttributes = new {
@class = "form-control" }, autofocus="autofocus"})
Or you can do that using jQuery:
@Html.EditorFor(model => model.Description)
@Html.ValidationMessageFor(model => model.Description)
$(function() {
$('.focus :input').focus();
});
Update:
As you know TextBoxFor
always creates a textbox with type input, But EditorFor
is a little bit smart, it renders markup based on the datatype of the property.