我已经创建了两个过滤器搜索框,并且我在else语句的帮助下将值传递给了控制器。在我的if语句中,我创建了两个单独的条件,将返回值传递到视图页面。在两个不同条件下,我只需要一个返回值。
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页
@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-->
}
那样的东西?
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);
}
编辑:
好吧,我不明白您想要什么,请尝试以下操作:
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();
}
您可以做:
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);
我认为您根本不需要第一个if语句
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"));
}
并将视图模型签名更改为IEnumerable
而不是List
。这是使用接口而不是实现的更好的编码方法。