美好的一天。我是MVC的新手,我想知道如何才能做到这一点,我希望用户使用帐号填充文本框,当用户输入帐号时,事件必须触发MVC操作并将该值传递给控制器,执行验证并将结果返回到视图并在其他文本框中填充结果。我的情况是我将一个帐号传递给一个Web服务,然后验证帐号,然后获得3个额外的字段(公司名称,公司注册号和邮政编码)并将其填入文本框中。请看看我到目前为止的情况,感谢您的帮助。
[HttpGet]
public ActionResult EmmAccountVerification(RegisterViewModel model)
{
customerDetail_PortTypeClient client = new customerDetail_PortTypeClient();
var accountnumber = model.EMMAccount;
var results = client.getCustomerContactDetails(accountnumber);
if (results != null)
{
model.CompanyRegistrationNumber = results.companyRegistartionNo;
model.CompanyName = results.fullName;
//results.companyRegistartionNo = model.CompanyRegistrationNumber;
//results.fullName = model.CompanyName;
}
return Json(model);
}
查看
@using (Html.BeginForm("Register", "Home", FormMethod.Post, new { @class = "form-horizontal", role = "form",id = "regform" }))
@Html.TextBoxFor(m => m.EMMAccount, new { @placeholder = "example: 0123456789", @class = "form-control", @type = "number", id = "txtA" , onkeypress = "capLock(event)",onblur = "CallWebService()" })
脚本
function CallWebService() {
debugger;
$("#txtA").blur(function () {
$.ajax({
url: "/EmmAccountVerification/Home",
type: "POST",
data: $("#regForm").serialize(),
dataType: "json"
}).done(function (model) {
$("#companyname").val(model.CompanyName);
$("#companyRegNum").val(model.CompanyRegistrationNumber);
$("#PostalCode").val(model.PostalCode);
});
});
答案 0 :(得分:0)
此外,您需要为post方法创建模型并为其设置值并[将其添加到帖子请求
var RegisterViewMode = { "EMMAccount": document.getElementById('txtA').value };
您的代码将显示链接 -
function CallWebService() {
debugger;
$("#txtA").blur(function () {
$.ajax({
url: "/EmmAccountVerification/Home",
type: "POST",
data: RegisterViewMode ,
dataType: "json"
}).done(function (model) {
$("#companyname").val(model.CompanyName);
$("#companyRegNum").val(model.CompanyRegistrationNumber);
$("#PostalCode").val(model.PostalCode);
});
});