如何使用名称或ID控制器搜索产品列表

时间:2013-08-09 09:57:57

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 asp.net-mvc-2

我想根据搜索加载视图中的产品列表。如果用户输入名称或ID,则应显示产品。我尝试过,但我只能做其中一个。

这就是我的尝试。

控制器

public class SearchController : Controller
    {

        private storeEntities db = new storeEntities();

        public ActionResult Find()
            {
            var det = (from d in db.products
                       select d).ToList();
            return View(det);

            }
        [HttpPost]
        public ActionResult Find(int pid)
        {
            var det = (from d in db.products
                       where d.id == pid
                       select d).ToList();

            return View(det);
        }
    }

查看

@model IEnumerable<SearchAppMvc.Models.product>

@{
    ViewBag.Title = "Find";
}

<h2>Find</h2>
<div>
@Html.Partial("_SearchPartial")
</div>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            category
        </th>
        <th>
            productName
        </th>
        <th>
            price
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.category)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.productName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.price)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.id }) |
            @Html.ActionLink("Details", "Details", new { id=item.id }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.id })
        </td>
    </tr>
}

部分视图

@using (Html.BeginForm( new RouteValueDictionary {{"controller","Search"},{"action","Find"},{"id","pid"}}))
{
    @*@Html.Editor("name")*@

    @Html.TextBox("pid")

    <input type="submit" value="search"/>                        
}

并使用实体框架工作完成模型

4 个答案:

答案 0 :(得分:1)

在您的控制器中:

[HttpPost]
public ActionResult Find(string searchString)
{

    var det = db.products.ToList();

    if (!String.IsNullOrEmpty(searchString))
    {
        det = det.Where(d=>d.productname.ToUpper().Contains(searchString.ToUpper()) ||
        d.productID == int.TryParse(searchString, out result))
    }

    return View(det)

}

在您的视图中:

@using (Html.BeginForm())
{
 <p>
     @Html.TextBox("SearchString");
     <input type="submit" value="Find"/>
 </p>
}

答案 1 :(得分:0)

   [HttpPost]
        public ActionResult Find(string pid)
        {
            var det = (from d in db.products
                       where d.id == Convert.Toint32(Pid) || d.name = pid
                       select d).ToList();

            return View(det);
        }
        }

@using (Html.BeginForm("Results", "Search"))
{
 @Html.TextBox("SearchText")
 <input class="search-button1" type="submit" value="Search" />
}

您的方法只接受一个参数,尝试将字符串传递给您的方法,并在查询中使用 OR 运算符将其与ID或名称进行比较。希望它有所帮助。

答案 2 :(得分:0)

我会有类似以下的ProductSearchModel类:

ProductSearchClass{
    public int ProductId{get;set;}
    public string ProductName{get;set;}
}

并让Controller操作接受该模型的副本:

public ActionResult Find(ProductSearchModel criteria){
    //Do the search logic here based on which property of the model is filled
    //Return whatever view you want to return
}

只需确保在您的视图中,ProductId和ProductName字段与搜索模型的属性具有相同的名称。然后,框架将根据用户填写的内容为您填充。

答案 3 :(得分:0)

得到解决方案

[HttpPost]
        public ActionResult Find(string pid)
        {

            int x;
            bool resl = int.TryParse(pid,out x);

            if (resl ==true)
            {
                var det = (from d in db.products
                           where d.id == x
                           select d).ToList();
                return View(det);
            }
            else 
            {
                var det = (from d in db.products
                           where d.productName == pid
                           select d).ToList();
                return View(det);
            }

        }