MVC DropDownList SelectedValue无法正确显示

azo*_*orr 20 asp.net-mvc html.dropdownlistfor asp.net-mvc-3

我试过搜索,没有发现任何解决我问题的方法.我在Razor视图上有一个DropDownList,它不会显示我在SelectList中标记为Selected的项目.以下是填充列表的控制器代码:

var statuses  = new SelectList(db.OrderStatuses, "ID", "Name", order.Status.ID.ToString());
ViewBag.Statuses = statuses;
return View(vm);
Run Code Online (Sandbox Code Playgroud)

这是查看代码:

<div class="display-label">
   Order Status</div>
<div class="editor-field">
   @Html.DropDownListFor(model => model.StatusID, (SelectList)ViewBag.Statuses)
   @Html.ValidationMessageFor(model => model.StatusID)
</div>
Run Code Online (Sandbox Code Playgroud)

我遍历它,甚至在视图中它具有正确的SelectedValue但是DDL始终显示列表中的第一个项目,而不管选择的值如何.任何人都可以指出我做错了让DDL默认为SelectValue吗?

Dar*_*rov 48

SelectList忽略构造函数的最后一个参数(您希望能够传递所选值id),因为DropDownListFor帮助程序使用您作为第一个参数传递的lambda表达式并使用特定属性的值.

所以这是丑陋的方式:

模型:

public class MyModel
{
    public int StatusID { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        // TODO: obviously this comes from your DB,
        // but I hate showing code on SO that people are
        // not able to compile and play with because it has 
        // gazzilion of external dependencies
        var statuses = new SelectList(
            new[] 
            {
                new { ID = 1, Name = "status 1" },
                new { ID = 2, Name = "status 2" },
                new { ID = 3, Name = "status 3" },
                new { ID = 4, Name = "status 4" },
            }, 
            "ID", 
            "Name"
        );
        ViewBag.Statuses = statuses;

        var model = new MyModel();
        model.StatusID = 3; // preselect the element with ID=3 in the list
        return View(model);
    }
}
Run Code Online (Sandbox Code Playgroud)

视图:

@model MyModel
...    
@Html.DropDownListFor(model => model.StatusID, (SelectList)ViewBag.Statuses)
Run Code Online (Sandbox Code Playgroud)

这是正确的方法,使用真实视图模型:

模型

public class MyModel
{
    public int StatusID { get; set; }
    public IEnumerable<SelectListItem> Statuses { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        // TODO: obviously this comes from your DB,
        // but I hate showing code on SO that people are
        // not able to compile and play with because it has 
        // gazzilion of external dependencies
        var statuses = new SelectList(
            new[] 
            {
                new { ID = 1, Name = "status 1" },
                new { ID = 2, Name = "status 2" },
                new { ID = 3, Name = "status 3" },
                new { ID = 4, Name = "status 4" },
            }, 
            "ID", 
            "Name"
        );
        var model = new MyModel();
        model.Statuses = statuses;
        model.StatusID = 3; // preselect the element with ID=3 in the list
        return View(model);
    }
}
Run Code Online (Sandbox Code Playgroud)

视图:

@model MyModel
...    
@Html.DropDownListFor(model => model.StatusID, Model.Statuses)
Run Code Online (Sandbox Code Playgroud)

  • @ppumkin,不,当你使用视图模型和强类型帮助器时,你不需要SelectList构造函数的第四个参数.这就是`x => x.StatusID`的作用.顺便提一下,从我的例子中我可以看到我甚至没有使用SelectList构造函数,因为我的视图模型属性是`IEnumerable <SelectListItem>`并且可以直接绑定到DropDownListFor. (2认同)