MVC DropDownList SelectedValue 显示不正确 [英] MVC DropDownList SelectedValue not displaying correctly

查看:19
本文介绍了MVC DropDownList SelectedValue 显示不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试搜索,但没有找到任何可以解决我的问题的内容.我在 Razor 视图上有一个 DropDownList,它不会显示我在 SelectList 中标记为 Selected 的项目.这是填充列表的控制器代码:

var statuses = new SelectList(db.OrderStatuses, "ID", "Name", order.Status.ID.ToString());ViewBag.Statuses = 状态;返回视图(vm);

这是查看代码:

订单状态

<div class="editor-field">@Html.DropDownListFor(model => model.StatusID, (SelectList)ViewBag.Statuses)@Html.ValidationMessageFor(model =>model.StatusID)

我遍历它,即使在视图中它也有正确的 SelectedValue,但是 DDL 总是显示列表中的第一项,而不管选择的值如何.谁能指出我在让 DDL 默认为 SelectValue 时做错了什么?

解决方案

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

所以这是一种丑陋的方法:

型号:

公共类 MyModel{公共 int StatusID { 获取;放;}}

控制器:

公共类 HomeController : 控制器{公共 ActionResult 索引(){//TODO:显然这来自您的数据库,//但我讨厌在 SO 上展示代码//无法编译和播放,因为它有//大量外部依赖var statuses = new SelectList(新的[]{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",名称");ViewBag.Statuses = 状态;var model = new MyModel();模型.StatusID = 3;//预选列表中 ID=3 的元素返回视图(模型);}}

查看:

@model MyModel...@Html.DropDownListFor(model => model.StatusID, (SelectList)ViewBag.Statuses)

<小时>

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

型号

公共类 MyModel{公共 int StatusID { 获取;放;}公共 IEnumerable状态 { 获取;放;}}

控制器:

公共类 HomeController : 控制器{公共 ActionResult 索引(){//TODO:显然这来自您的数据库,//但我讨厌在 SO 上展示代码//无法编译和播放,因为它有//大量外部依赖var statuses = new SelectList(新的[]{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",名称");var model = new MyModel();模型.状态 = 状态;模型.StatusID = 3;//预选列表中 ID=3 的元素返回视图(模型);}}

查看:

@model MyModel...@Html.DropDownListFor(model => model.StatusID, Model.Statuses)

I tried searching and didn't find anything that fixed my problem. I have a DropDownList on a Razor view that will not show the the item that I have marked as Selected in the SelectList. Here is the controller code that populates the list:

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

Here is the View code:

<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>

I walk through it and even in the view it has the correct SelectedValue however the DDL always shows the first item in the list regardless of the selected value. Can anyone point out what I am doing wrong to get the DDL to default to the SelectValue?

解决方案

The last argument of the SelectList constructor (in which you hope to be able to pass the selected value id) is ignored because the DropDownListFor helper uses the lambda expression you passed as first argument and uses the value of the specific property.

So here's the ugly way to do that:

Model:

public class MyModel
{
    public int StatusID { get; set; }
}

Controller:

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);
    }
}

View:

@model MyModel
...    
@Html.DropDownListFor(model => model.StatusID, (SelectList)ViewBag.Statuses)


and here's the correct way, using real view model:

Model

public class MyModel
{
    public int StatusID { get; set; }
    public IEnumerable<SelectListItem> Statuses { get; set; }
}

Controller:

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);
    }
}

View:

@model MyModel
...    
@Html.DropDownListFor(model => model.StatusID, Model.Statuses)

这篇关于MVC DropDownList SelectedValue 显示不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆