我试图在主索引视图中放置搜索栏,以便我可以搜索特定的客户端。我会做一个名字一个搜索,它工作正常,但如果我再次尝试搜索,我得到..在MVC 4中搜索并在相同的视图中重复
服务器错误“/”应用。无法找到该资源。
说明:HTTP 404。您正在查找的资源(或 它依赖一个)可能已被删除,更名,或者是 暂时不可用。请检查以下URL并确定 拼写正确。
请求的URL:/客户端/客户端/指数
我希望能够重复搜索,并返回到主索引为好。
Controller类
public class ClientController : Controller
{
private VolumeV2Context db = new VolumeV2Context();
//
// GET: /Client/
public ActionResult Index(string SearchParam)
{
if (SearchParam == null)
{
//just load the main index
return View(db.Clients.Take(25).ToList());
}
else
{
//search for the client name
var clients = db.Clients.Where(c => c.FirstName.Contains(SearchParam) || c.LastName.Contains(SearchParam)).Take(10).ToList();
return View(clients);
}
}
索引视图 我在这里使用的局部视图,以及用于通过线每个客户端线。我认为这将使它更容易为我
@model IEnumerable<VolumeV2.Models.Clients>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<form action="Client/Index" method="get" >
<input type="text" name="SearchParam" />
<input type="submit" value="Search" />
<table class="client" id ="results">
<tr>
<th>
@Html.DisplayNameFor(model => model.FirstName)
</th>
<th>
@Html.DisplayNameFor(model => model.LastName)
</th>
<th>
@Html.DisplayNameFor(model => model.PhoneNumber)
</th>
<th>
@Html.DisplayNameFor(model => model.Email)
</th>
<th>
@Html.DisplayNameFor(model => model.EmailList)
</th>
<th>
@Html.DisplayNameFor(model => model.HairType)
</th>
<th>
@Html.DisplayNameFor(model => model.Description)
</th>
<th></th>
</tr>
@foreach(var item in Model){
@Html.Partial("SearchedClients",item)
}
</table>
</form>
管窥
@model VolumeV2.Models.Clients
<tr >
<td>
@Html.DisplayFor(model => model.FirstName)
</td>
<td>
@Html.DisplayFor(model => model.LastName)
</td>
<td>
@Html.DisplayFor(model => model.PhoneNumber)
</td>
<td>
@Html.DisplayFor(model => model.Email)
</td>
<td>
@Html.DisplayFor(model => model.EmailList)
</td>
<td>
@Html.DisplayFor(model => model.HairType)
</td>
<td>
@Html.DisplayFor(model => model.Description)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = Model.Id }) |
@Html.ActionLink("Details", "Details", new { id = Model.Id }) |
@Html.ActionLink("Delete", "Delete", new { id = Model.Id }) |
@Html.ActionLink("Address", "GetAddress", new { id = Model.Id })
</td>
</tr>
表单动作你有一个返回作为'返回视图(“索引”,“客户端”,客户端)'和另一个'返回视图(“索引”,“客户端”,...)' - 他们复制粘贴你的代码?如果是这样,_index_可能不应该有一个小的_i_,并且_client_不应该有一个小的_c_。更好的是,你实际上并不需要指定它们,因为你要返回的'View'与你的动作名称相同,所以你所需要的只是'返回View(clients)' – 2013-03-04 23:57:02
正确指出,我已经做了变化,我现在得到一个新的错误。当我尝试进行第二次搜索时,Url路径发生变化。我现在更新帖子以显示我的新错误。 – Fpanico 2013-03-05 03:32:37