在MVC 4中搜索并在同一视图中重复

时间:2013-03-04 23:26:07

标签: c# asp.net-mvc-4

我正在尝试在主索引视图中放置一个搜索栏,以便我可以搜索特定的客户端。我会搜索一个名字并且工作正常,但如果我再次尝试搜索,我会...

  

“/”应用程序中的服务器错误。无法找到资源。

     
    

描述:HTTP 404.您正在寻找的资源(或其中之一)     它的依赖关系)可能已被删除,其名称已更改,或者是     暂时不可用。请查看以下网址并制作     确保它拼写正确。

         

请求的网址:/客户端/客户端/索引

  

我希望能够重复搜索并返回主索引。

控制器类

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>

1 个答案:

答案 0 :(得分:0)

将表单操作从“/ Client / Index”更改为“../ Client / Index”