有没有办法用 Razor 创建 DropDownList 并将第二个参数作为 Linq 结果而不是默认的空项传递?
关键是我正在使用累加器函数(“函数”),所以我不能将 null 传递给它(作为该问题的答案 -多个 IF/CASE 语句的替代方案)
如果 DropDownList 不合适,我真的很感激另一种选择。
控制器:
public ActionResult Index(string searchFullName, string searchExtension, string searchProject)
{
var extensionList = new List<string>();
var projectList = new List<string>();
var projects = from n in unitofwork.DomainRepository.Get()
select n.Project;
var extensions = from n in unitofwork.DomainRepository.Get()
select n.Extension;
extensionList.AddRange(extensions.Distinct());
projectList.AddRange(projects.Distinct());
ViewBag.searchproject = new SelectList(projectList);
ViewBag.searchExtension = new SelectList(extensionList);
return View(unitofwork.DomainRepository.Filter(n => n.Name.Contains(searchFullName), n => n.Extension == searchExtension));
}
“过滤”方法:
public virtual IEnumerable<T> Filter(params Expression<Func<T, bool>>[] filters)
{
IQueryable<T> query = dbSet;
return filters.Aggregate(query, (a, b) => a.Where(b));
}
看法:
@using (Html.BeginForm()) {
<p>
Name: @Html.TextBox("searchFullName")
Extension: @Html.DropDownList("searchExtension", "All") <- *would like to get one extension or all extensions*
Projects: @Html.DropDownList("searchProject","All")
<input type="submit" value="Filters" />
</p> }