Html.DropDownList - 如何将额外的添加到列表中

时间:2022-09-16 20:29:59

I am using Html.DropDownList to create an options list like so

我正在使用Html.DropDownList创建一个类似的选项列表

Html.DropDownList("LogType", new SelectList(Model.LogTypeList, "ID", "Name", Model.SelectedLogType),"-- ALL --");

As you can see I pass in a list but also pass in an extra argument to add an additional option: "-- All --". The result is as follows:

如您所见,我传入一个列表,但也传入一个额外的参数来添加一个额外的选项:“ - All - ”。结果如下:

<select name="LogType" id="LogType">
<option value="">-- ALL -- </option>
<option value="1">Debug</option>
<option value="2" selected="selected">Error</option>
</select>

How do I give -- All -- an value of 0 without having to manually construct the drop down list myself?

如何在不必手动构建下拉列表的情况下给出 - 全部 - 值为0?

3 个解决方案

#1


5  

I'm not sure but, why don't you construct the list just before your @html.DropDownList

我不确定,但是,为什么不在@html.DropDownList之前构建列表

var myList = new SelectList(Model.LogTypeList, "ID", "Name", Model.SelectedLogType);

and then append your desired item to it.

然后将所需的项目添加到其中。

var myList.add(new selectItem("-- ALL --",0));

and finally pass it to your syntax and see what this will result

最后将它传递给你的语法,看看会产生什么结果

Html.DropDownList("LogType",myList);

sorry, this is a quick answer using my iPhone, not sure if it will fix your problem, but I tried to help as much as I could.

对不起,这是使用我的iPhone的快速回答,不确定它是否能解决您的问题,但我尽可能多地帮助。

#2


3  

I tried to do this before and end with using jQuery:

我之前尝试过这样做,最后使用jQuery:

$("#LogType").append($("<option />").val("0").html("-- All --"));

#3


1  

Just for a different approach, I was amazed at how many answers on forums there were for this same question, all of which seemed overly complex for what can be achieved easily in WebForms with AppendDataItems (granted, it is webforms!).

仅仅针对不同的方法,我对同一个问题的论坛上有多少答案感到惊讶,所有这些答案对于使用AppendDataItems(被授予,它是webforms!)的WebForms中可以轻松实现的内容似乎过于复杂。

This worked well for me a few moments ago, keeping everything inline and keeping the view and model concerns separate:

这对我来说很有用,保持所有内联并保持视图和模型关注点分开:

@Html.DropDownListFor(model => model.Operators,
                new SelectList(new[] { 
                    new {id=-1, name="Please select an operator"}}
                    .Union(Model.Operators
                            .Select( o=> new { id=o.Id, name=o.Name})
                    ), "id", "name")) 

Which renders out as :

其中呈现为:

<select id="Operators" name="Operators">
    <option value="-1">Please select an operator</option>
    <option value="1">Tony Bolton</option>
    <option value="2">Joe Bloggs</option>
</select>

Haven't checked performance etc though, but felt it was worth sharing.

虽然没有检查性能等,但觉得值得分享。

#1


5  

I'm not sure but, why don't you construct the list just before your @html.DropDownList

我不确定,但是,为什么不在@html.DropDownList之前构建列表

var myList = new SelectList(Model.LogTypeList, "ID", "Name", Model.SelectedLogType);

and then append your desired item to it.

然后将所需的项目添加到其中。

var myList.add(new selectItem("-- ALL --",0));

and finally pass it to your syntax and see what this will result

最后将它传递给你的语法,看看会产生什么结果

Html.DropDownList("LogType",myList);

sorry, this is a quick answer using my iPhone, not sure if it will fix your problem, but I tried to help as much as I could.

对不起,这是使用我的iPhone的快速回答,不确定它是否能解决您的问题,但我尽可能多地帮助。

#2


3  

I tried to do this before and end with using jQuery:

我之前尝试过这样做,最后使用jQuery:

$("#LogType").append($("<option />").val("0").html("-- All --"));

#3


1  

Just for a different approach, I was amazed at how many answers on forums there were for this same question, all of which seemed overly complex for what can be achieved easily in WebForms with AppendDataItems (granted, it is webforms!).

仅仅针对不同的方法,我对同一个问题的论坛上有多少答案感到惊讶,所有这些答案对于使用AppendDataItems(被授予,它是webforms!)的WebForms中可以轻松实现的内容似乎过于复杂。

This worked well for me a few moments ago, keeping everything inline and keeping the view and model concerns separate:

这对我来说很有用,保持所有内联并保持视图和模型关注点分开:

@Html.DropDownListFor(model => model.Operators,
                new SelectList(new[] { 
                    new {id=-1, name="Please select an operator"}}
                    .Union(Model.Operators
                            .Select( o=> new { id=o.Id, name=o.Name})
                    ), "id", "name")) 

Which renders out as :

其中呈现为:

<select id="Operators" name="Operators">
    <option value="-1">Please select an operator</option>
    <option value="1">Tony Bolton</option>
    <option value="2">Joe Bloggs</option>
</select>

Haven't checked performance etc though, but felt it was worth sharing.

虽然没有检查性能等,但觉得值得分享。