0

我想根据搜索加载视图中的产品列表。如果用户输入名称或 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

4 回答 4

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>
}
于 2013-08-09T11:32:56.307 回答
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 或 name 进行比较。希望能帮助到你。

于 2013-08-09T10:16:52.783 回答
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);
            }

        }
于 2013-08-09T12:43:11.360 回答
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 字段与搜索模型的属性具有相同的名称。然后,框架将根据用户填写的内容为您填写它们。

于 2013-08-09T11:58:42.180 回答