C# 在每个笔划上更新过滤器
我在MVC5中使用C#。尝试在每次按键时更新我的搜索结果。我写了我认为合乎逻辑的东西。在我看来,我编写了一些Ajax来与我的post控制器通信C# 在每个笔划上更新过滤器,c#,jquery,ajax,post,C#,Jquery,Ajax,Post,我在MVC5中使用C#。尝试在每次按键时更新我的搜索结果。我写了我认为合乎逻辑的东西。在我看来,我编写了一些Ajax来与我的post控制器通信 @using (Html.BeginForm("Index", "Directory", FormMethod.Post)) { <p> Search Employee: <input type="text" name="userName" onkeyup="filterTerm(
@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>
@使用(Html.BeginForm(“Index”,“Directory”,FormMethod.Post))
{
搜索员工:
}
函数过滤器项(值){
$.ajax({
类型:“POST”,
url:'@url.Action(“索引”,“目录”)',
数据:JSON.stringify({userName:value}),
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
成功:功能(msg){
$(“#senderContent”).html(msg.toString);
},
错误:函数(){
警报(“未连接!”);
}
});
}
这是我的控制器:
[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();
}
[HttpPost]
公共操作结果索引(字符串用户名)
{
/**********建立联系********/
DirectoryEntry dir=createDirectoryEntry();
DirectorySearcher search=新的DirectorySearcher(dir);
/****请参阅类构造函数****/
/********创建用于存储结果的列表***************/
列表用户=新列表();
字符串DisplayName=“”,SAMAccountName=“”,Mail=“”,Description=“”,Department=“”,TelephoneNumber=“”,Fax=“”;
/*******滤波器参数************/
search.Filter=“(&(&(&(objectCategory=Person)(objectClass=User))(anr=“+userName+”*)”;
SearchResultCollection searchresult=search.FindAll();
search.PropertiesToLoad.Add(“Displayname”);
search.PropertiesToLoad.Add(“SAMAccountName”);
search.PropertiesToLoad.Add(“邮件”);
search.PropertiesToLoad.Add(“说明”);
search.PropertiesToLoad.Add(“电话号码”);
search.PropertiesToLoad.Add(“传真”);
search.PropertiesToLoad.Add(“部门”);
/*****************筛选和填充列表***************/
if(searchresult!=null)
{
foreach(SearchResult中的SearchResult iResult)
{
ADUser userAttributes=新的ADUser(“,”,“,”,“,”,“,”,”);
foreach(iResult.Properties.PropertyNames中的字符串PropertyName)
{
foreach(iResult.GetDirectoryEntry().Properties[PropertyName]中的对象键)
{
尝试
{
开关(PropertyName.ToUpper())
{
案例“DISPLAYNAME”:
DisplayName=key.ToString();
userAttributes.Name=DisplayName;
打破
案例“SAMACCOUNTNAME”:
SAMAccountName=key.ToString();
userAttributes.DomainUserName=SAMAccountName;
打破
案例“邮件”:
Mail=key.ToString();
userAttributes.EmailAddress=邮件;
打破
案例“说明”:
Description=key.ToString();
userAttributes.JobDescription=描述;
打破
案例“电话号码”:
电话号码=key.ToString();
userAttributes.TelephoneNumber=电话号码;
打破
案例“传真”:
Fax=key.ToString();
userAttributes.FaxNumber=传真;
打破
案例“部门”:
Department=key.ToString();
userAttributes.Department=部门;
打破
}
}
捕获{}
}
}
添加(userAttributes);
}
返回视图(用户);
}
返回视图();
}
我一直在监视Fiddler中发生的事情,它没有标记问题,但是ajax错误消息一直在提示。在传入ajax之前,我已经测试了该值,它正在正确读取该值。只是想知道为什么我会被提示这个错误。这是我的Ajax中的一些东西吗 只是在ajax调用中为
数据:
字段设置对象是否可以修复它(而不是字符串化)?此外,您还需要在success函数中使用.toString()
而不是.toString
。是的,仍然不起作用。通过Fiddler没有回应。这可能是一个愚蠢的问题,但是查看
不创建HTML页面吗?你不应该改用Json吗?