我想要一个简单的下拉列表,其中包含值“升序”和“降序”,然后在 Index() 方法中按下搜索按钮时将它们作为 sortOrder 发送到控制器。我尝试了很多不同的东西,但无法让它发挥作用,我做错了什么。
PS 我不想把它变成一个模型,我只想从下拉列表中取回一个简单的“Asc”或“Desc”到 sortOrder 变量中。
用户控制器.cs
public ViewResult Index( string searchString, string sortOrder = "Asc")
{
var sortOrderParam = new[]
{
new SelectListItem { Value = "Asc", Text = "Ascending" },
new SelectListItem { Value = "Desc", Text = "Descending" }
};
var users = from u in db.TBL_SYS_USER
select u;
if (!string.IsNullOrEmpty(searchString))
{
users = users.Where(x => x.USER_FIRST_NAME.ToUpper().Contains(searchString.ToUpper()));
}
switch (sortOrder)
{
case "Desc":
users = users.OrderByDescending(u => u.USER_FIRST_NAME);
break;
default:
users = users.OrderBy(u => u.USER_FIRST_NAME);
break;
}
PopulateFieldDropdownList("");
return View(users.ToList());
}
索引.cshtml
@using (Html.BeginForm())
{
<p>
Search Criteria: @Html.TextBox("searchString")
Order By: @Html.DropDownList("sortOrderParam")
<input type="submit" value="Search"/>
</p>
}