我在我的網頁上有一個表格,要求輸入姓氏和名字。當用戶提交這個表單時,我希望數據被傳遞給我的控制器中的一個函數,在那裏它用於在我們的Active Directory中查找相應的用戶。然後,我想將匹配的電子郵件地址返回給Javascript函數,然後將該信息顯示在我的頁面上。這是否是「正確」的方式去做這件事,如果是的話,我將如何在我的控制器中構造一個函數來接受表單輸入,並將數據返回給我的客戶端JavaScript?如何將數據從表單傳遞到我的控制器,然後返回到我的客戶端JavaScript?
我至今在我的控制器:
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>
}
您可以使用Ajax.BeginForm返回數據 –
您可以發佈迄今爲止的JavaScript嗎? – DDelgro
到目前爲止,我還沒有任何js,因爲我不知道如何將變量從我的控制器傳遞給我的js。 –