AJAX找不到该资源
我正在使用C#asp.net mvc4并尝试进行ajax搜索。但是有一个错误,它说“找不到资源”。 我做错了什么 控制器AJAX找不到该资源,ajax,asp.net-mvc,Ajax,Asp.net Mvc,我正在使用C#asp.net mvc4并尝试进行ajax搜索。但是有一个错误,它说“找不到资源”。 我做错了什么 控制器 //Search [HttpPost] public ActionResult ContractSearch(string Name) { var contracts = db.Contracts.Include(c => c.DocType).Include(c => c.CreditType).Include(c
//Search
[HttpPost]
public ActionResult ContractSearch(string Name)
{
var contracts = db.Contracts.Include(c => c.DocType).Include(c => c.CreditType).Include(c => c.Bank).Include(c => c.UserProfile).Where(c => c.FirstName.Equals(Name));
return View(contracts.ToList());
}
看法
在控制器中添加以下内容。然后你的错误就会被纠正
public ActionResult ContractSearch()
{
return View();
}
对于搜索,您可以尝试以下示例
型号:
public class Person
{
public string Name { get; set; }
public string Country { get; set; }
}
public ActionResult SearchPerson()
{
return View();
}
[HttpPost]
public ActionResult SearchPerson(string searchString)
{
System.Collections.Generic.List<Person> lst = new List<Person>();
lst.Add(new Person { Name = "chamara", Country = "Sri Lanka" });
lst.Add(new Person { Name = "Arun", Country = "India" });
if (!string.IsNullOrEmpty(searchString))
{
lst = lst.AsEnumerable().Where(r => r.Name.Contains(searchString)).ToList();
}
string result = string.Empty;
result = "<p>Search Result<p>";
foreach (Person item in lst)
{
result = result + "<p> Names is: " + item.Name + " and Country is:" + item.Country + "<p>";
}
return Content(result, "text/html");
}
@model IEnumerable<Mvc4Test.Models.Person>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>SearchPerson</title>
<script src="@Url.Content("~/Scripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
</head>
<body>
@using (Ajax.BeginForm("SearchPerson", "Search", new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "searchresults" }))
{
@Html.TextBox("searchString")
<input type="submit" value="Search" />
}
<div id="searchresults">
</div>
</body>
</html>
控制器:
public class Person
{
public string Name { get; set; }
public string Country { get; set; }
}
public ActionResult SearchPerson()
{
return View();
}
[HttpPost]
public ActionResult SearchPerson(string searchString)
{
System.Collections.Generic.List<Person> lst = new List<Person>();
lst.Add(new Person { Name = "chamara", Country = "Sri Lanka" });
lst.Add(new Person { Name = "Arun", Country = "India" });
if (!string.IsNullOrEmpty(searchString))
{
lst = lst.AsEnumerable().Where(r => r.Name.Contains(searchString)).ToList();
}
string result = string.Empty;
result = "<p>Search Result<p>";
foreach (Person item in lst)
{
result = result + "<p> Names is: " + item.Name + " and Country is:" + item.Country + "<p>";
}
return Content(result, "text/html");
}
@model IEnumerable<Mvc4Test.Models.Person>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>SearchPerson</title>
<script src="@Url.Content("~/Scripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
</head>
<body>
@using (Ajax.BeginForm("SearchPerson", "Search", new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "searchresults" }))
{
@Html.TextBox("searchString")
<input type="submit" value="Search" />
}
<div id="searchresults">
</div>
</body>
</html>
public ActionResult SearchPerson()
{
返回视图();
}
[HttpPost]
公共操作结果SearchPerson(字符串searchString)
{
System.Collections.Generic.List lst=新列表();
lst.Add(新人{Name=“chamara”,Country=“斯里兰卡”});
lst.Add(新人{Name=“Arun”,Country=“India”});
如果(!string.IsNullOrEmpty(searchString))
{
lst=lst.AsEnumerable().Where(r=>r.Name.Contains(searchString)).ToList();
}
字符串结果=string.Empty;
result=“搜索结果”;
foreach(lst中的个人项目)
{
结果=结果+”名称为:“+item.Name+”,国家为:“+item.Country+””;
}
返回内容(结果,“文本/html”);
}
查看:
public class Person
{
public string Name { get; set; }
public string Country { get; set; }
}
public ActionResult SearchPerson()
{
return View();
}
[HttpPost]
public ActionResult SearchPerson(string searchString)
{
System.Collections.Generic.List<Person> lst = new List<Person>();
lst.Add(new Person { Name = "chamara", Country = "Sri Lanka" });
lst.Add(new Person { Name = "Arun", Country = "India" });
if (!string.IsNullOrEmpty(searchString))
{
lst = lst.AsEnumerable().Where(r => r.Name.Contains(searchString)).ToList();
}
string result = string.Empty;
result = "<p>Search Result<p>";
foreach (Person item in lst)
{
result = result + "<p> Names is: " + item.Name + " and Country is:" + item.Country + "<p>";
}
return Content(result, "text/html");
}
@model IEnumerable<Mvc4Test.Models.Person>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>SearchPerson</title>
<script src="@Url.Content("~/Scripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
</head>
<body>
@using (Ajax.BeginForm("SearchPerson", "Search", new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "searchresults" }))
{
@Html.TextBox("searchString")
<input type="submit" value="Search" />
}
<div id="searchresults">
</div>
</body>
</html>
@model IEnumerable
@{
布局=空;
}
搜索者
@使用(Ajax.BeginForm(“SearchPerson”、“Search”、新的AjaxOptions{HttpMethod=“Post”、UpdateTargetId=“searchresults”}))
{
@文本框(“搜索字符串”)
}
默认情况下,Ajax.BeginForm
是否使用GET请求而不是POST请求?控制器名称正确吗?Ajax.begin使用POST请求,控制器名称正确。可能与您遇到的问题相同。干杯