我使用的是 MVC3 架构,c#.net。当焦点更改到下一个字段(即密码字段)时,我需要立即将文本框内容(用户 ID)与数据库进行比较。所以我想对 User Id 字段使用 onblur 事件,然后调用 Controller 方法。谁能告诉我如何解决这个问题?作为新手,非常感谢代码片段。
提前致谢
巴山
最佳答案
这是一个例子。 Controller 方法示例
[HttpPost] // can be HttpGet
public ActionResult Test(string id)
{
bool isValid = yourcheckmethod(); //.. check
var obj = new {
valid = isValid
};
return Json(obj);
}
这将是您的 javascript 函数。
function checkValidId(checkId)
{
$.ajax({
url: 'controllerName/Test',
type: 'POST',
contentType: 'application/json;',
data: JSON.stringify({ id: checkId }),
success: function (valid)
{
if(valid) {
//show that id is valid
} else {
//show that id is not valid
}
}
});
}
关于c# - 如何从 MVC3 中的 javascript 调用 Controller 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11644658/