无法让Dropdownlist正常工作

时间:2013-08-16 00:25:37

标签: asp.net-mvc razor html-select

我想要一个简单的下拉列表,其中包含值“Ascending”和“Descending”,然后在Index()方法中按下搜索按钮时将其作为sortOrder发送到控制器。我尝试了很多不同的事情,但是无法让它发挥作用,我做错了什么。

P.S我不想把它变成一个模型,我只想从下拉列表中找到一个简单的“Asc”或“Desc”到sortOrder变量。

UserController.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());
    }

Index.cshtml

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

        <input type="submit" value="Search"/>
    </p>
}

2 个答案:

答案 0 :(得分:1)

在Controller Action中,您应该创建DropDownList选项列表,并将它们传递给ViewBag中的View,或者作为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>,它们是@using (Html.BeginForm()) { <p> Search Criteria: @Html.TextBox("searchString") Order By: @Html.DropDownList("sortOrderParam", ViewBag.SortOrderOptions) <input type="submit" value="Search"/> </p> } DropDownList中的选项。 因此,您的视图将如下所示:

{{1}}

答案 1 :(得分: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>
}