简体   繁体   English

如何在模型包含下拉列表的mvc.net中绑定模型?

[英]How to bind a model in mvc.net where the model contains a list of dropdowns?

If I have a ViewModel with the following structure: 如果我有一个具有以下结构的ViewModel:

public class FormViewModel
{
    public string Name { get; set; }
    public List<OptionGroup> Options { get; set; }
    public string Comments { get; set; }
}

public class OptionGroup
{
    public string OptionType;
    public IEnumerable<SelectListItem> Options { get; set; }
    public string SelectedOption;
}

and I want to post to a Controller Method with a signature like: 我想发布到具有如下签名的Controller方法:

    [HttpPost]
    public ActionResult PostForm(FormViewModel model)
    {
        // do stuff
    }

How do I bind the SelectLists in the razor such that the selected values are properly bound when sending back to the server? 如何在剃须刀中绑定SelectList,以便在将其发送回服务器时正确绑定所选值?

My first instinct was to just try: 我的第一个直觉是尝试:

        foreach (OptionGroup optionGroup in Model.Options)
        {
            <div class="form-group">
                <label>@optionGroup.OptionType</label>
                @Html.DropDownListFor(m => optionGroup.SelectedOption, optionGroup.Options, optionGroup.OptionType, new { @class = "form-control" })
            </div>
        }

But that results in not getting any options returns to the server at all. 但这导致根本没有任何选项返回到服务器。

Then I found this article by Scott Hanselman: 然后,我找到了Scott Hanselman的这篇文章:

http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx

and tried using a for loop instead: 并尝试使用for循环:

        for (int i = 0; i < Model.Options.Count; i++)
        {
            <div class="form-group">
                <label>@Model.Options[i].OptionType</label>
                @Html.DropDownListFor(m => m.Options[i].SelectedOption, Model.Options[i].Options, Model.Options[i].OptionType, new { @class = "form-control", name = string.Format("model.Options[{0}].SelectedOption", i) })
            </div>
        }

This binds the options list, but fails to populate the selected values. 这将绑定选项列表,但无法填充所选值。 (ie I get a list of Options on the server, but all the properties of the options are Null) (即,我在服务器上获得了一个选项列表,但是选项的所有属性均为Null)

All the examples I can find are using a simple enumerable as the whole view model - how do you bind to a list of dropdowns when they are only part of the complete ViewModel? 我能找到的所有示例都使用一个简单的可枚举作为整个视图模型-当下拉列表只是完整ViewModel的一部分时,如何绑定到下拉列表?

Any pointers would be great, 任何指针都很棒,

Cheers! 干杯!


EDIT 编辑

Related: How Do I Model Bind A List Of 'List<SelectItem>' Using MVC.Net which is sending a key value also 相关: 我如何使用也发送键值的MVC.Net对“ List <SelectItem>”的列表进行绑定建模

and taking into account Stephen Muecke's answer I have tried: 并考虑到Stephen Muecke的回答,我尝试过:

        for (int i = 0; i < Model.Options.Count; i++)
        {
            <div class="form-group">
                <label>@Model.Options[i].OptionType</label>
                @Html.HiddenFor(m => Model.Options[i].OptionType)
                @Html.DropDownListFor(m => m.Options[i].SelectedOption, Model.Options[i].Options, Model.Options[i].OptionType, new { @class = "form-control" })
            </div>
        }

but still no luck :( 但仍然没有运气:(

Remove the following from the method 从方法中删除以下内容

.. name = string.Format("model.Options[{0}].SelectedOption", i) ..

@Html.DropDownList() method will correctly name the select for you, which will be @Html.DropDownList()方法将为您正确命名选择,即

<select name="Options[0].SelectedOption" ...>
<select name="Options[1].SelectedOption" ...>
// etc

but you are perpending "model." 但您是“模特”。 to it so the names will not match up on postback 以便名称在回发时不匹配

Your properties for OptionGroup are also missing accessors 您的OptionGroup属性也缺少访问器

public class OptionGroup
{
  public string OptionType { get; set; } // add get/set
  public IEnumerable<SelectListItem> Options { get; set; }
  public string SelectedOption { get; set; } // add get/set
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM