[英]How to pass a single return value by using if else condition with the help of MVC?
我創建了兩個過濾器搜索框,並在if 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-->
}
你可以做:
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);
那這樣的事呢?
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();
}
我認為您根本不需要第一個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
。 這是使用接口而不是其實現進行編碼的一種更好的方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.