Asp.net mvc 3 WebGrid()中MVC 3 DropDownList()的问题

Asp.net mvc 3 WebGrid()中MVC 3 DropDownList()的问题,asp.net-mvc-3,drop-down-menu,webgrid,Asp.net Mvc 3,Drop Down Menu,Webgrid,我试图在WebGrid中放置一个DropDownList,但我不知道如何操作: 我尝试过的事情: grid.Column("Id", "Value", format: ((item) => Html.DropDownListFor(item.SelectedValue, item.Colors))) 及 及 还有其他各种各样的,但我无法让它工作。 非常感谢您的帮助 模型 控制器 看法 你应使用: Colors = new SelectList(colorList, "ColorID

我试图在WebGrid中放置一个DropDownList,但我不知道如何操作:

我尝试过的事情:

grid.Column("Id", "Value", format: ((item) =>
   Html.DropDownListFor(item.SelectedValue, item.Colors)))

还有其他各种各样的,但我无法让它工作。 非常感谢您的帮助

模型 控制器 看法 你应使用:

Colors = new SelectList(colorList, "ColorID", "ColorName")
另外,为所有行项目定义相同的SelectList对我来说似乎有点浪费,尤其是当它们包含相同的值时。我会稍微重构一下您的视图模型:

public class MyViewModel
{
    public IEnumerable<SelectListItem> Colors { get; set; }
    public IEnumerable<PersonViewModel> People { get; set; }
}

public class PersonViewModel
{
    public string Name { get; set; }
    public int Age { get; set; }
    public int SelectedValue { get; set; }
}
并且认为:

@model MyViewModel

@{
    var grid = new WebGrid(Model.People);
}

<h2>DropDownList in WebGrid</h2>

@using (Html.BeginForm())
{
    @grid.GetHtml(
        columns: grid.Columns(
            grid.Column("Name"),
            grid.Column("Age"),
            grid.Column(
                "dropdown", 
                format: @<span>@Html.DropDownList("Color", Model.Colors)</span>
            )
        )
    )    
    <p>
        <button>Submit</button>
    </p>
}
你应使用:

Colors = new SelectList(colorList, "ColorID", "ColorName")
另外,为所有行项目定义相同的SelectList对我来说似乎有点浪费,尤其是当它们包含相同的值时。我会稍微重构一下您的视图模型:

public class MyViewModel
{
    public IEnumerable<SelectListItem> Colors { get; set; }
    public IEnumerable<PersonViewModel> People { get; set; }
}

public class PersonViewModel
{
    public string Name { get; set; }
    public int Age { get; set; }
    public int SelectedValue { get; set; }
}
并且认为:

@model MyViewModel

@{
    var grid = new WebGrid(Model.People);
}

<h2>DropDownList in WebGrid</h2>

@using (Html.BeginForm())
{
    @grid.GetHtml(
        columns: grid.Columns(
            grid.Column("Name"),
            grid.Column("Age"),
            grid.Column(
                "dropdown", 
                format: @<span>@Html.DropDownList("Color", Model.Colors)</span>
            )
        )
    )    
    <p>
        <button>Submit</button>
    </p>
}

..使我的下拉列表在加载时从列表中选择一个值。以下操作确实有效。我自己也试过了。任何问题都可以随时通知我

@Html.DropDownList("RejectReason", Model.SalesReasonList.Select(u => new SelectListItem
                {
                    Text = u.Text,
                    Value = u.Value,
                    Selected = u.Value ==@item.RejectReason
                }))

..使我的下拉列表在加载时从列表中选择一个值。以下操作确实有效。我自己也试过了。任何问题都可以随时通知我

@Html.DropDownList("RejectReason", Model.SalesReasonList.Select(u => new SelectListItem
                {
                    Text = u.Text,
                    Value = u.Value,
                    Selected = u.Value ==@item.RejectReason
                }))

@Snæbjørn,我稍微修改了我的答案,在最后添加了一个示例,说明如何在您的POST操作中提交表单时检索所选值。您好Darin,我如何制作下拉列表,以便在加载表单时从列表中选择值。@Snæbjørn,我稍微修改了我的答案,在最后添加了一个示例,说明如何在POST操作中提交表单时检索所选值。您好,Darin,我如何制作下拉列表,以便在加载表单时从列表中选择值。
public class MyViewModel
{
    public IEnumerable<SelectListItem> Colors { get; set; }
    public IEnumerable<PersonViewModel> People { get; set; }
}

public class PersonViewModel
{
    public string Name { get; set; }
    public int Age { get; set; }
    public int SelectedValue { get; set; }
}
public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            // define the values used to render the dropdown lists
            // for each row
            Colors = new[]
            {
                new SelectListItem { Value = "1", Text = "Green" },
                new SelectListItem { Value = "2", Text = "Red" },
                new SelectListItem { Value = "3", Text = "Yellow" },
            },

            // this is the collection we will be binding the WebGrid to
            People = new[]
            {
                new PersonViewModel { Name = "Foo", Age = 42 },
                new PersonViewModel { Name = "Bar", Age = 1337 },
            }
        };

        return View(model);
    }
}
@model MyViewModel

@{
    var grid = new WebGrid(Model.People);
}

<h2>DropDownList in WebGrid</h2>

@using (Html.BeginForm())
{
    @grid.GetHtml(
        columns: grid.Columns(
            grid.Column("Name"),
            grid.Column("Age"),
            grid.Column(
                "dropdown", 
                format: @<span>@Html.DropDownList("Color", Model.Colors)</span>
            )
        )
    )    
    <p>
        <button>Submit</button>
    </p>
}
grid.Column(
    "dropdown", 
    format: 
        @<span>
            @{ var index = Guid.NewGuid().ToString(); }
            @Html.Hidden("People.Index", index)
            @Html.DropDownList("People[" + index + "].SelectedValue", Model.Colors)
        </span>
)
[HttpPost]
public ActionResult Index(MyViewModel model)
{
    // the model variable will contain the People collection
    // automatically bound for each row containing the selected value
    ...
}
@Html.DropDownList("RejectReason", Model.SalesReasonList.Select(u => new SelectListItem
                {
                    Text = u.Text,
                    Value = u.Value,
                    Selected = u.Value ==@item.RejectReason
                }))