2013-07-21 60 views
0

我正在使用C#asp.net mvc4並嘗試執行ajax搜索。但它是錯誤的,它說「找不到資源」。 我做錯了什麼?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 
+0

是否'默認Ajax.BeginForm'使用GET請求而不是POST請求?控制器名稱是否正確? – David

+0

Ajax.BeginForm使用POST請求,控制器名稱正確。 – tarakan

+0

也許[這](http://stackoverflow.com/questions/4476511/asp-net-mvc-ajax-beginform-is-not-submitting-via-ajax)是你有同樣的問題。乾杯! –

回答

0

添加下面的控制器。那麼你的錯誤將被糾正。

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> 
+0

我通過使用PartialView解決了這個問題。 – tarakan