-1

我想要一个简单的下拉列表,其中包含值“升序”和“降序”,然后在 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>
}
4

2 回答 2

1

在您的控制器操作中,您应该创建 DropDownList 选项列表并将它们传递给 ViewBag 中的视图,或者作为 ViewModel 的属性。如下所示:

public ViewResult Index(string searchString, string sortOrder = "Asc")
{
    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("");

    var sortOrderOptions = new List<SelectListItem>();
    sortOrderOptions.Add(new SelectListItem { Value = "Asc", Text = "Ascending" });
    sortOrderOptions.Add(new SelectListItem { Value = "Desc", Text = "Descending" });
    ViewBag.SortOrderOptions = sortOrderOptions;

    return View(users.ToList());
}

方法中的第一个参数Html.DropDownList是字段的名称(这将是要发布到您的 Action 的值的名称),第二个参数是IEnumerable<SelectListItem>DropDownList 中的选项。因此,您的视图将如下所示:

@using (Html.BeginForm())
{
    <p>
        Search Criteria: @Html.TextBox("searchString")
        Order By: @Html.DropDownList("sortOrderParam", ViewBag.SortOrderOptions)

        <input type="submit" value="Search"/>
    </p>
}
于 2013-08-16T01:09:46.473 回答
0

我使用了以下代码,最终弄清楚了:

控制器:

public ViewResult Index(string searchString, string fieldToSearch, string sortOrder = "Asc")
    {
        var sortOrderOptions = new[]
            {
                "Asc",
                "Desc"
            };

        ViewBag.sortOrderList = new SelectList(sortOrderOptions);

        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("sortOrder", ViewBag.sortOrderList as SelectList)
    <input type="submit" value="Search"/>
</p>
}
于 2013-08-16T01:32:27.640 回答