【问题标题】:ajax. The resource cannot be found阿贾克斯。没有找到您要查的资源
【发布时间】:2013-07-21 01:29:28
【问题描述】:

我正在使用 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 =>          c.Bank).Include(c => c.UserProfile).Where(c => c.FirstName.Equals(Name));
        return View(contracts.ToList());
    }

查看

@model IEnumerable<CreditoriyaApp.Models.Contract>

@{
ViewBag.Title = "Index";
}

<div>
@using (Ajax.BeginForm("ContractSearch", "Contract", new AjaxOptions { UpdateTargetId = "searchresults" }))
{
<input type="text" name="Name" />
<input type="submit" value="Search" />
}

<div id="searchresults">
@if (Model != null && Model.Count()>0)
{
    <ul>
    @foreach (var item in Model)
    { 
        <li>@item.FirstName</li>
    }
    </ul>
}
</div>

错误

Server Error in '/' Application.

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed,   had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is  spelled correctly. 

Requested URL: /Contract/ContractSearch

【问题讨论】:

  • Ajax.BeginForm 默认使用 GET 请求而不是 POST 请求吗?控制器名称是否正确?
  • Ajax.BeginForm 使用 POST 请求,控制器名称正确。
  • 也许this 与您遇到的问题相同。干杯!

标签: ajax asp.net-mvc


【解决方案1】:

在您的控制器中添加以下内容。然后您的错误将得到纠正。

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>

【讨论】:

    猜你喜欢
    • 2010-10-11
    • 1970-01-01
    • 1970-01-01
    • 2018-05-15
    • 2018-07-04
    • 1970-01-01
    • 2014-07-01
    • 2023-03-21
    • 2015-01-13
    相关资源
    最近更新 更多