简体   繁体   English

将复杂模型绑定到MVC中的下拉列表

[英]Binding a complex model to a drop down list in MVC

I'm trying to bind a model that has two properties - one Int, and one Boolean - to a drop-down list in MVC. 我正在尝试将具有两个属性(一个Int和一个布尔值)的模型绑定到MVC中的下拉列表。 The boolean is a discriminator and the integer an ID. 布尔值是鉴别符,整数是ID。 It is not possible to split the drop down list in two. 无法将下拉列表一分为二。

Here is my code so far. 到目前为止,这是我的代码。

<select class="col-md-3 form-control" name="Model.ID" id="model-select">
    <option value="0" selected>Select an Option</option>
    @foreach (var m in models.OrderBy(x => x.Id))
    {
        <option value="@m.ID" data-discriminator="@m.Discriminator">
            @m.Name
        </option>
    }
</select>

The model looks something like this 该模型看起来像这样

class MyModel
{
  int ID { get; set; }
  string Name { get; set; }
  boolean Discriminator { get; set; }
}

The aim is to provide a set of models to the View, then the user can pick one of these. 目的是为View提供一组模型,然后用户可以选择其中的一个。 Unfortunately each model has two properties which are used to identify which model was selected in the database - the Id, which mirrors the Id in the database, and the Discriminator. 不幸的是,每个模型都有两个属性,用于标识在数据库中选择了哪个模型-标识数据库中ID的Id和Discriminator。 The two types are otherwise incompatible in the database, hence the discriminator. 否则这两种类型在数据库中是不兼容的,因此是区分符。 For the sake of design, I only want to have these two in the same drop-down list, as you can only select one at a time anyway. 为了设计起见,我只想将这两个放在同一个下拉列表中,因为无论如何一次只能选择一个。

My idea of a solution was to create 2 hidden fields which would be bound to the model like so 我的解决方案想法是创建2个隐藏字段,将其像这样绑定到模型

<input type="hidden" name="Model.ID" />
<input type="hidden" name="Model.Discriminator" />

These would be updated via JavaScript and then bound to the model (as far as I know, using names like that will bind it correctly, providing that the destination property on the model passed to the POST is Model in this example). 这些将通过JavaScript进行更新,然后绑定到模型(据我所知,使用这样的名称将其正确绑定,前提是在此示例中,传递给POST的模型上的destination属性是Model)。

Are there any other alternatives I could pursue? 我还有其他选择吗?

EDIT: Also worth noting that this 'Model' is part of a more complex model and is not the only field being POSTed, so if that makes any difference... 编辑:还值得注意的是,此“模型”是更复杂模型的一部分,并且不是唯一要发布的字段,因此,如果有任何区别...

A select box is only going to be able to post one thing, and using JavaScript to populate hidden fields, while perhaps a workable solution, seems very brittle. 一个选择框将只能发布一件事,并且使用JavaScript填充隐藏的字段,虽然可能是一个可行的解决方案,但似乎非常脆弱。 Your best bet would like be creating an intermediary property that you can bind to and include both sets of information as the option value: 最好的选择是创建一个可以绑定到的中介属性,并将这两组信息都包含为选项值:

public string SelectedThing
{
    get { return string.Format("{0},{1}", ID, Discriminator); }
    set
    {
        if (value != null)
        {
            var parts = value.Split(',');
            if (parts.Length == 2)
            {
                Int32.TryParse(parts[0], out ID);
                Boolean.TryParse(parts[1], out Discriminator);
            }
        }
    }
}

Then you would need to compose your select list in a similar way: 然后,您将需要以类似的方式撰写选择列表:

ViewBag.MyModelChoices = myModels.Select(m => new SelectListItem
    {
        Value = string.Format("{0},{1}", m.ID, m.Discriminator),
        Text = m.Name
    });

And finally, you would bind to this new property in your view: 最后,您将绑定到该新属性:

@Html.DropDownListFor(m => m.SelectedThing, ViewBag.MyModelChoices)

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

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