我的Index.cshtml文件中有一个用户名文本框,每当用户更改文本框中的文本时,我想检查 Active Directory 中的匹配项,然后可能将其显示在 中DropDownList,以便用户可以从其中进行选择。
因此,我在文本框事件上调用 JavaScript 函数oninput,现在的问题是如何从该 JS 函数调用我的 C#FindUser()方法Index.cshtml.cs?我已经尝试了很多我读过的内容,ajax调用不起作用,添加方法[WebMethod]并使方法静态不起作用,并且互联网上的大多数都是我没有使用的 MVC。
文本框位于Index.cshtml:
<input type="text" class="form-control" oninput="findUsers()" />
Run Code Online (Sandbox Code Playgroud)
JavaScript 函数:
function findUsers() {
$.ajax({
url: 'Index\FindUser',
success: function (data) {
alert(data);
},
error: function (error) {
alert("Error: " + error);
}
})
}
Run Code Online (Sandbox Code Playgroud)
导致浏览器报警
错误:[对象对象]
Index.cshtml.cs中的方法:
public void FindUser()
{
// code
}
Run Code Online (Sandbox Code Playgroud)
该方法甚至不是从 JS 函数中调用的。
另外,我也读过几次,从视图调用 C# 方法并不是正确的方法。如果是的话,我怎样才能实现我的目标呢?