我在MVC中使用C#5.尝试在每次击键时更新我的搜索结果。我写过我认为合乎逻辑的内容。在我的视图中,我写了一些Ajax来与我的帖子控制器进行通信。
@using (Html.BeginForm("Index", "Directory", FormMethod.Post))
{
<p>
Search Employee: <input type="text" name="userName" onkeyup="filterTerm(this.value);" />
</p>
}
<script>
function filterTerm(value) {
$.ajax({
type: "POST",
url: '@Url.Action("Index","Directory")',
data: JSON.stringify({ userName: value }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
$("#senderContent").html(msg.toString);
},
error: function () {
alert("not connected!");
}
});
}
</script>
这是我的控制者:
[HttpPost]
public ActionResult Index(string userName)
{
/**********Establish Connection********/
DirectoryEntry dir = createDirectoryEntry();
DirectorySearcher search = new DirectorySearcher(dir);
/****Refer to class constructor****/
/********Create the List to store results in***************/
List<ADUser> Users = new List<ADUser>();
string DisplayName = "", SAMAccountName = "", Mail = "", Description = "", Department = "", TelephoneNumber = "", Fax = "";
/*******Filter parameters************/
search.Filter = "(&((&(objectCategory=Person)(objectClass=User)))(anr=" + userName + "* ))";
SearchResultCollection searchresult = search.FindAll();
search.PropertiesToLoad.Add("Displayname");
search.PropertiesToLoad.Add("SAMAccountName");
search.PropertiesToLoad.Add("Mail");
search.PropertiesToLoad.Add("Description");
search.PropertiesToLoad.Add("TelephoneNumber");
search.PropertiesToLoad.Add("Fax");
search.PropertiesToLoad.Add("Department");
/*****************Filtering and populating the List****************/
if (searchresult != null)
{
foreach (SearchResult iResult in searchresult)
{
ADUser userAttributes = new ADUser("", "", "", "", "", "", "");
foreach (string PropertyName in iResult.Properties.PropertyNames)
{
foreach (Object key in iResult.GetDirectoryEntry().Properties[PropertyName])
{
try
{
switch (PropertyName.ToUpper())
{
case "DISPLAYNAME":
DisplayName = key.ToString();
userAttributes.Name = DisplayName;
break;
case "SAMACCOUNTNAME":
SAMAccountName = key.ToString();
userAttributes.DomainUserName = SAMAccountName;
break;
case "MAIL":
Mail = key.ToString();
userAttributes.EmailAddress = Mail;
break;
case "DESCRIPTION":
Description = key.ToString();
userAttributes.JobDescription = Description;
break;
case "TELEPHONENUMBER":
TelephoneNumber = key.ToString();
userAttributes.TelephoneNumber = TelephoneNumber;
break;
case "FAX":
Fax = key.ToString();
userAttributes.FaxNumber = Fax;
break;
case "DEPARTMENT":
Department = key.ToString();
userAttributes.Department = Department;
break;
}
}
catch { }
}
}
Users.Add(userAttributes);
}
return View(Users);
}
return View();
}
我一直在监视Fiddler中发生的事情并且它没有标记问题,但是ajax错误消息会一直提示。我在传递给ajax之前测试了这个值,它正在正确读取值。只是想知道为什么我被提示这个错误。它是我的Ajax中的东西吗?