使用DropDownListFor

时间:2016-12-13 17:01:06

标签: c# asp.net asp.net-mvc

我正在尝试使用视图模型填充下拉列表,然后在HTTPPOST上尝试检索所选值,但模型在后期控制器中没有数据绑定。任何帮助,将不胜感激。

查看模型

public class TransferViewModel
{

    public IEnumerable<User> Users { get; set; }
    public string SortOrder { get; set; }
    public SelectList Departments { get; set; }
    public SelectList Sites { get; set; }

    public SelectListItem SelectedDepartment { get; set; }
    public SelectListItem SelectedSite { get; set; }
    public User SelectedUser { get; set; }
}

控制器

public ActionResult Sort(TransferViewModel model)
    {
        var users = from s in _db.Users select s;
        var departments = new SelectList(_db.Departments);
        var sites = new SelectList(_db.Sites);
        model.Departments = departments;
        model.Sites = sites;

        if (model.SelectedDepartment != null)
        {
            users = users.Where(s => s.Department.ID == Convert.ToInt32(model.SelectedDepartment));
        }
        else if (model.SelectedSite != null)
        {
            users = users.Where(s => s.Site.ID == Convert.ToInt32(model.SelectedSite));
        }

        switch (model.SortOrder)
        {
            case "name_desc":
                users = users.OrderByDescending(s => s.LName);
                break;
            case "dept":
                users = users.OrderBy(s => s.Department.Name);
                break;
            case "dept_desc":
                users = users.OrderByDescending(s => s.Department.Name);
                break;
            case "site":
                users = users.OrderBy(s => s.Site.Name);
                break;
            case "site_desc":
                users = users.OrderByDescending(s => s.Site.Name);
                break;
            default:
                users = users.OrderBy(s => s.LName);
                break;
        }
        model.Users = users;
        model.Departments = new SelectList(_db.Departments, "ID", "Name", model.SelectedDepartment);
        model.Sites = new SelectList(_db.Sites,"ID","Name", model.SelectedSite);
        return View(model);
    }

查看

@using (Html.BeginForm("Sort", "Transfer", FormMethod.Post ))
{
<p>
    Display by @Html.DropDownListFor(model => model.SelectedDepartment, Model.Departments, htmlAttributes: new { @class = "form-control"})
    Department
</p>
<p>
    or by Location: @Html.DropDownListFor(model => model.SelectedSite, Model.Sites, htmlAttributes: new { @class = "form-control"})

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

1 个答案:

答案 0 :(得分:1)

您需要在模型中拥有int属性,因为在帖子中下拉列表的选定ID将被发回给控制器:

public class TransferViewModel
{
    ......................
    ......................
    public int SelectedDepartment { get; set; }
    public int SelectedSite { get; set; }
    .....................
    .....................
}

您在行动中告诉自己要使用ID列将下拉列表绑定为DataValueMember

// see that ID argument there that is binding the value which will be posted back
model.Departments = new SelectList(_db.Departments, "ID", "Name", model.SelectedDepartment);
model.Sites = new SelectList(_db.Sites,"ID","Name", model.SelectedSite);