Reputation: 3429
i have created two filter search box and i have passed the value in to the controller with the help of if else statement. In my if statement i have created two separate condition to pass the return value to the view page. I need only one return value for two different conditions.
public ActionResult ViewCompany(string Company, string City)
{
//View All Records
var data = dp.Company.SqlQuery("Select * from CompanyRegistration ORDER BY CompanyID DESC").ToList();
//return View(dp.Company.Where(x => x.CompanyName.Contains(searching) || searching == null).ToList());
//Search Function
var customers1 = from s in dp.Company select s;
if (Company != null || City != null)
{
if (!String.IsNullOrEmpty(Company))
{
customers1 = customers1.Where(s => s.CompanyName.Contains(Company));
return View(customers1.ToList());
}
if (!String.IsNullOrEmpty(City))
{
customers1 = customers1.Where(s => s.City.Contains(City));
return View(customers1.ToList());
}
}
return View(data);
}
csHTML page
@Html.BeginForm("ViewCompany", "Company", FormMethod.Get)
{
<div class="modal fade" id="upload3Modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="modal-close" data-dismiss="modal" aria-label="Close">
<i class="font-icon-close-2"></i>
</button>
<h4 class="modal-title" id="myModalLabel">Search Company</h4>
</div>
<div class="modal-upload menu-big-icons">
<div class="modal-upload-cont">
<div class="modal-upload-cont-in" style="border-left: none;">
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="tab-upload-3-1">
<br />
<br />
Company Name: @Html.TextBox("Company")
<br />
<br />
City: @Html.TextBox("City")
<br />
<br />
<input type="submit" name="submit" value="Search" class=" btn btn-rounded btn-inline btn-success" />
</div><!--.tab-pane-->
</div><!--.tab-content-->
</div><!--.modal-upload-cont-in-->
</div><!--.modal-upload-cont-->
</div>
</div>
</div>
</div><!--.modal-->
}
Upvotes: 0
Views: 1218
Reputation: 85
What about something like that ?
public ActionResult ViewCompany(string Company, string City)
{
//View All Records
var data = dp.Company.SqlQuery("Select * from CompanyRegistration ORDER BY CompanyID DESC").ToList();
//return View(dp.Company.Where(x => x.CompanyName.Contains(searching) || searching == null)).ToList());
//Search Function
var customers1 = from s in dp.Company select s;
if (Company != null || City != null)
{
return !String.IsNullOrEmpty(Company) ?
View(customers1.Where(s => s.CompanyName.Contains(Company)).ToList())
: View(customers1.Where(s => s.City.Contains(City)).ToList());
}
return View(data);
}
Edit :
Ok, I didn't understand what you wanted, try this :
if (Company != null || City != null)
{
if (!string.IsNullOrEmpty(Company))
{
customers1 = (!string.IsNullOrEmpty(City)) ?
customers1.Where(s => s.CompanyName.Contains(Company) && s.City.Contains(City))
: customers1.Where(s => s.CompanyName.Contains(Company));
}
if (!string.IsNullOrEmpty(City))
{
customers1 = (!string.IsNullOrEmpty(Company)) ?
customers1.Where(s => s.CompanyName.Contains(Company) && s.City.Contains(City))
: customers1.Where(s => s.City.Contains(City));
}
return customers1.ToList();
}
Upvotes: 1
Reputation: 2964
You could do :
var companies = from s in dp.Company select s;
if(!String.IsNullOrEmpty(Company)) companies = companies.Where(s => s.CompanyName.Contains(Company));
if(!String.IsNullOrEmpty(City)) companies = companies.Where(s => s.City.Contains(City));
return View(companies);
Upvotes: 1
Reputation: 330
I think that you don't need the first if statement at all
public ActionResult ViewCompany(string Company, string City)
{
// Don't do redurant work
//Search Function
var customers = from s in dp.Company select s;
if (!String.IsNullOrEmpty(Company))
{
customers = customers.Where(s => s.CompanyName.Contains(Company));
return View(customers);
}
if (!String.IsNullOrEmpty(City))
{
customers = customers.Where(s => s.City.Contains(City));
return View(customers);
}
return View(dp.Company.SqlQuery("Select * from CompanyRegistration ORDER BY CompanyID DESC"));
}
And change your view-model signature to IEnumerable
instead of List
. It's much a better approach of coding using the interfaces instead of their implementations.
Upvotes: 0