2013-07-21 2 views
0

저는 C# asp.net mvc4를 사용 중이며 아약스 검색을 시도하고 있습니다. 그러나 ther는 오류이며 "리소스를 찾을 수 없습니다."라고 표시됩니다. 내가 뭘 잘못하고있어?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

어쩌면 [this] (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

관련 문제