在 mvc4 中为 dropdownlistfor 选择的值 [英] Selected value for dropdownlistfor in mvc4

查看:30
本文介绍了在 mvc4 中为 dropdownlistfor 选择的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Edit 操作中创建了一个 ViewBag 选择列表,并将要选择的值设置为:

I have created a ViewBag selectlist in Edit action and set the value to be selected as:

ViewBag.Doors = new SelectList(
    new[]
    {
        new {ID = 1, Name="1-Door"},
        new {ID = 2, Name="2-Doors"},
        new {ID = 3, Name="3-Doors"},
        new {ID = 4, Name="4-Doors"},
        new {ID = 5, Name="5-Doors"},
        new {ID = 6, Name="6-Doors"},
        new {ID = 7, Name="7-Doors"}
    },
    "ID", "Name", advert.Doors);

但在视图中,默认情况下未选择下拉列表的值.

But in the view the value for the dropdown is not selected by default.

我的查看代码是:

@Html.DropDownListFor(model => model.Doors, (SelectList)ViewBag.Doors, "--Select--")
@Html.ValidationMessageFor(model => model.Doors)

属性 Doors 将是数字 1,2,3,..

The property Doors will be numeric 1,2,3,..

推荐答案

我将如何在 ViewBag 中使用 Anonymous 类型?

过载

控制器动作方法

public ActionResult DropDownListFor()
{
    ViewBag.Doors = new SelectList(
                        new[]
                        {
                            new {Value = 1,Text="1-Door"},
                            new {Value = 2,Text="2-Door"},
                            new {Value = 3,Text="4-Door"},
                            new {Value = 4,Text="4-Door"},
                            new {Value = 5,Text="5-Door"},
                            new {Value = 6,Text="6-Door"},
                            new {Value = 7,Text="7-Doors"}
                        }, "Value", "Text", 7);
    return View();
}

查看

@Html.DropDownList("Doors")










动作方法

[HttpGet]
public ActionResult DropDownListFor()
{
    List<SelectListItem> items = new List<SelectListItem>();

    items.Add(new SelectListItem { Text = "Action", Value = "0" });
    items.Add(new SelectListItem { Text = "Drama", Value = "1" });
    items.Add(new SelectListItem { Text = "Comedy", Value = "2", 
                                                             Selected = true });
    items.Add(new SelectListItem { Text = "Science Fiction", Value = "3" });
    ViewBag.MovieType = items;

    return View();
}

查看

@using (Html.BeginForm("Action", "Controller", FormMethod.Post))
{
    @Html.DropDownList("MovieType")
}










查看

@using (Html.BeginForm("Action", "Controller", FormMethod.Post))
{
    @Html.DropDownListFor(m => m.Id, Model.DDLList, "Please select");
}

动作方法

[HttpGet]
public ActionResult DropDownListFor()
{
    return View(new Models.Dropdown());
}

查看模型

public class Dropdown
{
    public string Id { get; set; }
    public List<SelectListItem> DDLList
    {
        get
        {
            return new List<SelectListItem>() 
            { 
                new SelectListItem
                { 
                    Text = "1-Door", 
                    Value = "1", 
                    Selected = true
                },
                new SelectListItem
                { 
                    Selected = false, 
                    Value = "2", 
                    Text = "2-Door"
                }
            };
        }
    }
}

这篇关于在 mvc4 中为 dropdownlistfor 选择的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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