0

如您所见,我有一种从表中检索字段的方法。在我看来,我在两个文本框中进行搜索。

ClientID是一个字符串,所以没问题。但是OrderId是一个Int,所以我使用 ToString() 但它仍然不起作用。

我为 OrderId 编写第二个 IF 条件的方式错了吗?谢谢你的帮助

public ActionResult Search(string searchString, string searchOrder)
{  
    var user = from m in db.Order   
               select m;

    if (!String.IsNullOrEmpty(searchString))
    {
        user = user.Where(s => s.Order.ClientID.Contains(searchString));
    }         

    if (!String.IsNullOrEmpty(searchOrder))
    {
        user = user.Where(c => c.Order.OrderId.ToString().Contains(searchOrder));
    }

    return this.View("Order", "PrintView", user);
}
4

2 回答 2

2

听起来您应该解析searchOrder为整数,然后使用:

user = user.Where(c => c.Order.OrderId == targetOrderId);

除了别的,你真的想要一个searchOrder“15”来匹配订单 2150 吗?这听起来不太可能有用。

于 2012-09-05T09:12:47.673 回答
0

要使用数字搜索,请将相关方法更改为以下内容:

 public ActionResult PrintOrders(string searchString, int searchOrder = 0)
    {
        var user = from m in db.OrderDetails   
                   select m;

        if (!String.IsNullOrEmpty(searchString))
        {
            user = user.Where(s => s.Order.ClientID.Contains(searchString));               
        }

        if (searchOrder > 0)
        {
            user = user.Where(c => c.Order.OrderId == searchOrder);               
        }

        return this.ViewPdf("Order", "PrintView", user);
    }

另一种方法是:

    public ActionResult PrintOrders(string searchString, string searchOrder)
    { 
        var user = from m in db.OrderDetails   
                   select m;

        if (!String.IsNullOrEmpty(searchString))
        {
            user = user.Where(s => s.Order.ClientID.Contains(searchString));               
        }

        int tmp = Int32.Parse(searchOrder);

        if (tmp != 0)
        {
            user = user.Where(c => c.Order.OrderId == tmp);
        }

        return this.ViewPdf("Order", "PrintView", user);
    }

视图看起来像这样:

@using (Html.BeginForm("PrintOrders", "Historic", FormMethod.Get, new { target = "_blank" }))
{

   Search by Client ID : @Html.TextBox("searchString")

   Search by Order ID : @Html.TextBox("searchOrder")

  <input type="submit" value="Search" />
}
于 2012-09-05T10:38:47.323 回答