Reputation: 103
I create the search from table in asp.net mvc , but it is working with the string only, when i change to the int and customer_mobile there some error coming . how i can search by number or int ?
Controller:
public ActionResult Index(string seraching)
{
return View(_context.Customers.Where(x => x.Customer_Name.Contains(seraching) || seraching == null).ToList());
}
view :
@Html.TextBox("seraching")<input type="submit" value="Search" />
@if (Model.Count() == 0)
{
<tr>
<td colspan="3" style="color:red">
No Match Any Document
</td>
</tr>
}
else
{
foreach (var item in Model)
{
Model:
public class Customers
{
[Key]
public int Customer_Id { get; set; }
public string Customer_Name { get; set; }
public int Customer_Mobile { get; set; }
Thank you
Upvotes: 1
Views: 2018
Reputation: 73
public ActionResult Index(int? seraching)
{
if (seraching.HasValue){
return View(_context.Customers.Where(x=>x.Customer_Mobile.ToString().Contains(seraching.ToString())).ToList());}
else{
return View(_context.Customers.ToList());}
}
Convert int to string and use contains like you were using.
Upvotes: 2
Reputation: 23
Why not try to change the parameter to an int and also change the where clause to search by the attribute that is also an int (for example an Id if it is declared as an int in the Customers model)
Also, you can ditch the .Contains and instead use a simple == operator when comparing integers, therefore resulting in the following:
public ActionResult Index(int seraching)
{
return View(_context.Customers.Where(x => (x.MobileNumber == seraching) || seraching == null).ToList());
}
Upvotes: 0