简体   繁体   English

mvc3 Html.DropDownListFor-在以下位置动态创建“ value”属性<option>

[英]mvc3 Html.DropDownListFor - dynamically create “value” attribute in <option>

I am trying to dynamically generate the "value" attribute of the tag using the HTML.DropDownListFor, but I am running into issues. 我正在尝试使用HTML.DropDownListFor动态生成标记的“值”属性,但是遇到了问题。 I am thinking of just hard-coding a for-loop and inserting raw HTML, but I am positive there is an easier way to do this (?) 我正在考虑仅对for循环进行硬编码并插入原始HTML,但我肯定有一种更简单的方法可以做到这一点(?)

I have a view model: 我有一个视图模型:

    public class DSDCustomerViewModel
        {
            public IEnumerable<dsd_l_chain> chainBuild { get; set; }
            public string Id_dsd { get; set; }
            public string Owner { get; set; }
            public IEnumerable<string> WFCategory { get; set; }
            public IEnumerable<string> TransactionType { get; set; } 
[etc etc etc]

the chainBuild property comes from my database (using EF) that I fill using an extension method for my view as follows: chainBuild属性来自我的数据库(使用EF),该数据库使用扩展方法填充视图,如下所示:

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <table>
        <tbody>
[etc etc]
<td colspan="1" rowspan="1">
                    Chain Name
                </td>
                <td colspan="1" style="vertical-align: top;">
                    @Html.DropDownListFor(model => model.cc_chainName, new SelectList(Model.cc_chainName), "Select ...")
                </td>
                <td rowspan="1" colspan="2" style="vertical-align: top;">
                    @Html.ValidationMessageFor(model => model.cc_chainName)
                </td>
[etc etc]

the cc_chainName EF object has the attributes "Chain_Desc", "Chain", "Chain_Group" and "ID" among others. cc_chainName EF对象具有“ Chain_Desc”,“ Chain”,“ Chain_Group”和“ ID”等属性。

The generated HTML is as follows: 生成的HTML如下:

<select id="cc_chainName" name="cc_chainName"><option value="">Select ...</option>
<option>1ST STOP</option>
<option>3 RIVERS ICE CREAM SPCLTS</option>
<option>3RIVERS SALES TO NON-NAP CUSTS</option>

How do I modify: 我该如何修改:

@Html.DropDownListFor(model => model.cc_chainName, new SelectList(Model.cc_chainName), "Select ...")

...so that the generated HTML code set the "value" attribute of the tag to cc_chainName.ID? ...,以便生成的HTML代码将标记的“值”属性设置为cc_chainName.ID? so that the generated HTML would look like this: 这样生成的HTML将如下所示:

<select id="cc_chainName" name="cc_chainName"><option value="">Select ...</option>
<option value="1">1ST STOP</option>
<option value="2">3 RIVERS ICE CREAM SPCLTS</option>
<option value="3">3RIVERS SALES TO NON-NAP CUSTS</option>

note the "value" tags contain the IDs from the cc_chainName model 请注意,“值”标记包含来自cc_chainName模型的ID

Thanks! 谢谢!

Each SelectList takes a dataValueField and dataTextField property in the constructor: 每个SelectList在构造函数中都带有dataValueFielddataTextField属性:

@Html.DropDownListFor(model => model.cc_chainName, 
                     new SelectList(Model.cc_chainName, 
                                    "ID", "TextualPropertyNameHere"), "Select ...")
                            //      ^^ id property and text property here

You didn't supply the text property name.. so the above assumes a model similar to this: 您没有提供文本属性名称。.因此,以上假设的模型与此类似:

class cc_chainName {
    public int ID { get; set; }
    public string TextualPropertyNameHere { get; set; }
}

EDIT: 编辑:

In response to your comment. 回应您的评论。

Consider this model: 考虑以下模型:

public class Product {
    public int Id { get; set; }
    public string ProductName { get; set; }
}

With it setup as an enumerable in a view: 将其设置为视图中的枚举:

@model IEnumerable<YourApplication.Models.Product>

..with this Controller action: ..使用此Controller动作:

var products = new List<Product>()
{
    new Product() { Id = 1, ProductName = "Lollies" },
    new Product() { Id = 2, ProductName = "Beer" },
    new Product() { Id = 3, ProductName = "Funny Hat" }
};

return View(products);

This DropDownList (notice the property names are strings in the SelectList ): DropDownList (注意,属性名称是SelectList中的字符串):

@Html.DropDownList("myDropDown", new SelectList(Model, "Id", "ProductName"), "-- Select item --")

Produces the following markup: 产生以下标记:

<select id="myDropDown" name="myDropDown">
    <option value="">-- Select item --</option>
    <option value="1">Lollies</option>
    <option value="2">Beer</option>
    <option value="3">Funny Hat</option>
</select>

To get what you wanted, you can explicitly pass "ProductName" in both strings.. like this: 要获得所需的内容,可以在两个字符串中显式传递“ ProductName”。

@Html.DropDownList("myDropDown", new SelectList(Model, "ProductName", "ProductName"), "-- Select item --")

Which produces: 产生:

<select id="myDropDown" name="myDropDown">
    <option value="">-- Select item --</option>
    <option value="Lollies">Lollies</option>
    <option value="Beer">Beer</option>
    <option value="Funny Hat">Funny Hat</option>
</select>

How would take into consideration distinct option elements? 如何考虑不同的期权要素? I have a DropDownList with some repeated elements from a Database. 我有一个DropDownList,其中有一些来自数据库的重复元素。 I can only seem to get either value attributes or distinct elements. 我似乎只能得到值属性或不同的元素。

Here's my code for that: 这是我的代码:

@Html.DropDownList("ResidentialBuilding", new SelectList(Model.Select(x => x.type).Distinct()))

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

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