asp.net-mvc - 在 mvc4 中为 dropdownlistfor 选择的值

标签 asp.net-mvc asp.net-mvc-3 asp.net-mvc-4

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

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

但是在 View 中,默认情况下未选择下拉列表的值。

我的 View 代码是:
@Html.DropDownListFor(model => model.Doors, (SelectList)ViewBag.Doors, "--Select--")
@Html.ValidationMessageFor(model => model.Doors)

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

最佳答案

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

重载

Controller Action 方法

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

我将如何使用 SelectListItem 做到这一点?

Action 方法
[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")
}

我将如何使用 View 模型来做到这一点?

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

Action 方法
[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"
                }
            };
        }
    }
}

关于asp.net-mvc - 在 mvc4 中为 dropdownlistfor 选择的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18507086/

相关文章:

c# - ASP.NET MVC 4 - 401 访问被拒绝

jquery - MVC 失去焦点时需要进行字段验证

c# - 如何在禁用 Javascript 时优雅地处理 AJAX PartialView 更新

asp.net-mvc - 找不到类型或命名空间名称 'DbContext'

asp.net - 为什么 ASP.NET MVC 3 无法正确验证我的 float ?

c# - 使用存储过程进行多重映射的 Dapper

c# - 使用表单序列化的 JSON 对象发布未映射到 C# 对象

c# - ASP.NET MVC 3 Razor View 限制

css - Bootstrap 图标未显示在已发布的 ASP.NET MVC 应用程序中

c# - ASP.Net MVC 4 API 自定义方法名称