LeoJ
LeoJ

Reputation: 57

Can't get event blur with asp.net mvc 3

Hi guys I've struggling how to get the event blur from a asp.net mvc 3 strongly typed view. This is what i've tried.

<table>
 <td class="contentTable">
     @Html.LabelFor(model => model.Ciudad)
  </td>
  <td>
     @Html.EditorFor(model => model.Ciudad, new{id= "mapSearch"})
     @Html.ValidationMessageFor(model => model.Ciudad)
 </td>
</table>


<script type="text/javascript">
    $(function () {
        $('#mapSearch').blur(function () {
           myFunction();
       });
});
</script>   

myFunction is already a function that I'm using fired by a button, but now I wanted it fired by an event. I've also tryed with:

@Html.EditorFor(model => model.Ciudad, new{onblur= "mapSearch();"})

Thanks

Upvotes: 1

Views: 2596

Answers (2)

Fred Christianson
Fred Christianson

Reputation: 151

EditorFor doesn't have an override that takes html attributes. At the "EditorFor" level it doesn't know what elements may be getting inserted or how to get the attributes to them. You need to use TextBoxFor, or DropDownListFor or another helper that adds a specific element. Or you need to add the attributes in your editor template if that's the editor you are using. You can pass attributes to your template if you want, but as part of the model (or ViewBag, or other ways).

Upvotes: 0

Shyju
Shyju

Reputation: 218732

Try this. Change your View Markup like this

 @Html.EditorFor(model => model.Ciudad)

and script like this

$(function () {
    $('#Ciudad').blur(function () {
           myFunction();
    });
});

Assuming you have a javascript function called myFunction exist

function myFunction() {
        console.debug("myFunction Called");
       //Some other awesome stuff here
}

JsFiddle sample : http://jsfiddle.net/C3wMY/4/

Upvotes: 2

Related Questions