Search code examples
c#asp.net-mvc-3ado.netsearch-form

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. But OrderId is an Int, so I use ToString() but it still don't work.

Am I wrong in the way I wrote my second IF condition for OrderId ? 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);
}

Solution

  • 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" />
    }