[英]My search method doen't work with number even with ToString()
As you can see, I have a method to retrieve fields from my table. 如您所见,我有一种从表中检索字段的方法。 In my view, I do the search in two textbox. 在我看来,我在两个文本框中进行搜索。
ClientID is a string , so no problem it works. ClientID是一个字符串 ,所以没问题。 But OrderId is an Int , so I use ToString() but it still don't work. 但是OrderId是一个Int ,所以我使用ToString()但它仍然不起作用。
Am I wrong in the way I wrote my second IF condition for OrderId ? 我在为OrderId编写第二个IF条件的方式错了吗? Thanks for your help 谢谢你的帮助
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);
}
It sounds like you should be parsing searchOrder
to an integer, then using: 听起来你应该将searchOrder
解析为整数,然后使用:
user = user.Where(c => c.Order.OrderId == targetOrderId);
Aside from anything else, would you really want a searchOrder
of "15" to match order 2150? 除了别的,你真的想要一个searchOrder
“15”的匹配订单2150? That sounds unlikely to be useful. 这听起来不太有用。
To search with number, change the method in question to the following : 要使用数字进行搜索,请将相关方法更改为以下内容:
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);
}
Another method is : 另一种方法是:
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);
}
And the view looks like that : 视图看起来像这样:
@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" />
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.