我的网页中有一个表格,要求输入名字和姓氏。当用户提交此表单时,我希望将数据传递到我的 Controller 中的一个函数,用于在我们的 Active Directory 中查找相应的用户。然后我想将匹配的电子邮件地址返回给 Javascript 函数,然后在我的页面上显示该信息。这是执行此操作的“正确”方法吗?如果是,我将如何在我的 Controller 中构造一个函数来接受表单输入,并将数据返回到我的客户端 javascript?
到目前为止我的 Controller 中有什么:
public SearchResult[] searchAD(String fname, String lname)
{
Func<System.DirectoryServices.ActiveDirectory.Domain> domain = System.DirectoryServices.ActiveDirectory.Domain.GetCurrentDomain;
System.DirectoryServices.DirectoryEntry root = new DirectoryEntry("LDAP://"+domain);
DirectorySearcher searcher = new DirectorySearcher();
searcher.SearchRoot = root;
searcher.SearchScope = SearchScope.Subtree;
searcher.Filter = string.Format("(&(objectCategory=person)(objectClass=user)(givenName={0})(sn={1}))", fname, lname);
SearchResult[] results = new SearchResult['a'];
searcher.FindAll().CopyTo(results, 0);
return results;
}
还有我的表格:
@using (Html.BeginForm("searchAD", "AD", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(m => m.firstName, "First Name", htmlAttributes: new { @class = "control-label" })
<div>
@Html.EditorFor(model => model.firstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.firstName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.lastName, "Last Name", htmlAttributes: new { @class = "control-label" })
<div>
@Html.EditorFor(model => model.lastName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.lastName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div>
<input type="submit" value="Search" class="btn" />
</div>
</div>
}
最佳答案
您可以将值存储在 TempData 中并在 View 中获取它
public ActionResult searchAD(String fname, String lname)
{
Func<System.DirectoryServices.ActiveDirectory.Domain> domain = System.DirectoryServices.ActiveDirectory.Domain.GetCurrentDomain;
System.DirectoryServices.DirectoryEntry root = new DirectoryEntry("LDAP://"+domain);
DirectorySearcher searcher = new DirectorySearcher();
searcher.SearchRoot = root;
searcher.SearchScope = SearchScope.Subtree;
searcher.Filter = string.Format("(&(objectCategory=person)(objectClass=user)(givenName={0})(sn={1}))", fname, lname);
SearchResult[] results = new SearchResult['a'];
searcher.FindAll().CopyTo(results, 0);
TempData["Result"]=results;
return View("ViewName");
}
在 View 中(Jquery)
$(function(){
if ('@TempData["Result"]' != '')
{
// call your function here and pass TempData Value
}
});
关于javascript - 如何将数据从表单传递到我的 Controller ,然后返回到我的客户端 javascript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42513092/