简体   繁体   English

ASP.NET MVC 4.5 OnBlur 事件文本框

[英]ASP.NET MVC 4.5 OnBlur event TextBox

I have a C# ASP.net MVC 4.5 solution.我有一个 C# ASP.net MVC 4.5 解决方案。 I want to create a form with automated address validation.我想创建一个带有自动地址验证的表单。 If you enter a zipcode and housenumber the streetname en city is looked up using an external REST API.如果您输入邮政编码和门牌号码,则会使用外部 REST API 查找街道名称 en city。

After filling in the housenumber there must be a call to a function in a controller (using AJAX) to look up the address, update the model data and show the result in the form.填写门牌号后,必须调用控制器中的函数(使用 AJAX)来查找地址、更新模型数据并在表单中显示结果。 I can't figure out how to do this.我无法弄清楚如何做到这一点。

I was thinking about doing a form post (using AJAX) fired when the onBlur from the housenumber is called.我正在考虑在调用来自房屋号码的 onBlur 时触发表单发布(使用 AJAX)。 But is this the best way ?但这是最好的方法吗? Is it really the best way is posting back the whole form ?发回整个表格真的是最好的方法吗?

Example so far:到目前为止的例子:

<div class="col-md-7">
 @Html.TextBoxFor(model => model.HouseNumber, new { @class = "form-control", @placeholder = "HouseNumber" })
 @Html.ValidationMessageFor(model => model.HouseNumber, null, new { @class = "text-danger" })
</div>

When the TextBox HouseNumber lost focus I want to call the function 'ValidateAddress' in my controller 'AddressBook'.当 TextBox HouseNumber 失去焦点时,我想在我的控制器“AddressBook”中调用函数“ValidateAddress”。 The function will lookup the streetname and city and will fill the model.Streetname and model.City fields.该函数将查找街道名称和城市,并将填充 model.Streetname 和 model.City 字段。

You can have blur event on HouseNumber textbox您可以在 HouseNumber 文本框上设置模糊事件

try this尝试这个

@Html.TextBoxFor(model => model.HouseNumber, new { @class = "form-control", @placeholder = "HouseNumber" onblur = "alert('call some javascript function')"})

or或者

better use jquery function更好地使用jquery函数

updated:更新:

    $("#HouseNumber").blur(function () {
         $.get("ControllerName/ActionMethod",{parameterName:value},function(data){
// extract values from data object and assign ut to your controls
});
});

Well, you could change your controller action to return the model as JSON:好吧,您可以更改控制器操作以将模型作为 JSON 返回:

public ActionResult ValidateAddress(YourModel model){
  // Do  your validation
  return Json(model); 
}

and then add an AJAX call to this action and fill your form in the callback:然后向此操作添加 AJAX 调用并在回调中填写您的表单:

$( "#HouseNumber" ).blur( function() {
  $.ajax({
    url: "/yourActionUrl",
    type: "POST",
    data: $("#yourForm").serialize(),
    dataType: "json"
  }).done(function( model ) {
    $("#Streetname").val(model.Streetname);
    $("#City").val(model.City);
  });
});

make your get Mathod on controller with same model parameter使您在具有相同模型参数的控制器上获得 Mathod

public ActionResult Login(LoginData model)
    {
        if (model == null)
        {
            model = new LoginData();
        }
        return View(model);
    }

after filling hoseno Call your function if you want you can use ajax and redirect to your getmethod and pass your model填充软管后调用您的函数,如果您愿意,您可以使用 ajax 并重定向到您的 getmethod 并传递您的模型

for validation use script validation so that you can remove or add according to your conditions.验证使用脚本验证,以便您可以根据您的条件删除或添加。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM