将 Html.DropDownList 与静态项绑定

发布于 2024-10-31 05:04:38 字数 163 浏览 0 评论 0原文

我必须静态地将 Html.DropDownList 与两个项目绑定。

Text="Yes" Value="1"
Text="No"  Value="0"

重要的是,我必须设置文本和值字段。

我该怎么做?

I have to bind an Html.DropDownList with just two items statically.

Text="Yes" Value="1"
Text="No"  Value="0"

The important thing is that, I have to set the text and value fields.

How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

鲜肉鲜肉永远不皱 2024-11-07 05:04:38

我用过这个可以正常工作

        @Html.DropDownList("Status", new List<SelectListItem>

                 {
                    new SelectListItem{ Text="Active", Value = "1" },
                    new SelectListItem{ Text="Not-Active", Value = "0" }
                 }) 

I used this is properly working

        @Html.DropDownList("Status", new List<SelectListItem>

                 {
                    new SelectListItem{ Text="Active", Value = "1" },
                    new SelectListItem{ Text="Not-Active", Value = "0" }
                 }) 
执笏见 2024-11-07 05:04:38

最佳实践是不要在视图中创建 SelectList。您应该在控制器中创建它并使用 ViewData 传递它。

示例:

var list = new SelectList(new [] 
{
    new { ID = "1", Name = "name1" },
    new { ID = "2", Name = "name2" },
    new { ID = "3", Name = "name3" },
}, 
"ID", "Name", 1);

ViewData["list"]=list;
return View();

您传递给构造函数:IEnumerable 对象、值字段、文本字段和选定的值。

在视图中:

<%=Html.DropDownList("list",ViewData["list"] as SelectList) %>

It is a best practice not to create the SelectList in the view. You should create it in the controller and pass it using the ViewData.

Example:

var list = new SelectList(new [] 
{
    new { ID = "1", Name = "name1" },
    new { ID = "2", Name = "name2" },
    new { ID = "3", Name = "name3" },
}, 
"ID", "Name", 1);

ViewData["list"]=list;
return View();

you pass to the constratctor: the IEnumerable objec,the value field the text field and the selected value.

in the View:

<%=Html.DropDownList("list",ViewData["list"] as SelectList) %>
铃予 2024-11-07 05:04:38

下面的代码假设您正在使用 razor 视图引擎,如果没有,您将需要对其进行转换。

@{
   var listItems = new List<ListItem>();
   listItems.Add(new ListItem{Text="Yes", Value="1"});
   listItems.Add(new ListItem{Text="No", Value="0"});
}

@Html.DropDownListFor(m=>m.SelectedValue, listItem);

您应该考虑在代码中创建模型而不是视图。这也是编辑器模板的一个很好的候选者。

Code below assumes you are using razor view engine if not you will need to convert it.

@{
   var listItems = new List<ListItem>();
   listItems.Add(new ListItem{Text="Yes", Value="1"});
   listItems.Add(new ListItem{Text="No", Value="0"});
}

@Html.DropDownListFor(m=>m.SelectedValue, listItem);

You should consider creating the model in your code instead of the view. Also this would be a good candidate for an editor template.

等风来 2024-11-07 05:04:38

如果你想明确一点然后尝试

        @{
            var domainsList = new SelectList(new []
            {
                new SelectListItem { Text = ".Com", Value = ".com", Selected = true },
                new SelectListItem { Text = ".Shopping", Value = ".shopping"},
                new SelectListItem { Text = ".Org", Value = ".org"},
                new SelectListItem { Text = ".Net", Value = ".net"},
                new SelectListItem { Text = ".AE", Value = ".ae"},
                new SelectListItem { Text = ".Info", Value = ".info"},
            }, "Value", "Text");
        }
        @Html.DropDownList("TopLevelDomains", domainsList)

if you want to be alittle explicity then try

        @{
            var domainsList = new SelectList(new []
            {
                new SelectListItem { Text = ".Com", Value = ".com", Selected = true },
                new SelectListItem { Text = ".Shopping", Value = ".shopping"},
                new SelectListItem { Text = ".Org", Value = ".org"},
                new SelectListItem { Text = ".Net", Value = ".net"},
                new SelectListItem { Text = ".AE", Value = ".ae"},
                new SelectListItem { Text = ".Info", Value = ".info"},
            }, "Value", "Text");
        }
        @Html.DropDownList("TopLevelDomains", domainsList)
遥远的绿洲 2024-11-07 05:04:38

这为我解决了这个问题:

 <td>
            @{  var RlistItems = new List<SelectListItem>();

                RlistItems.Add(new SelectListItem { Text = "Select Room Type", Value = "0" });
                RlistItems.Add(new SelectListItem { Text = "Meeting Room", Value = "1" });
                RlistItems.Add(new SelectListItem { Text = "Office", Value = "2" });
                RlistItems.Add(new SelectListItem { Text = "Cafeteria", Value = "3" });
            }

                @Html.DropDownListFor(model=>model.FirstOrDefault().RoomType
,RlistItems,RlistItems[item.RoomType.Value].Selected=true )
        </td>

This solved it for me:

 <td>
            @{  var RlistItems = new List<SelectListItem>();

                RlistItems.Add(new SelectListItem { Text = "Select Room Type", Value = "0" });
                RlistItems.Add(new SelectListItem { Text = "Meeting Room", Value = "1" });
                RlistItems.Add(new SelectListItem { Text = "Office", Value = "2" });
                RlistItems.Add(new SelectListItem { Text = "Cafeteria", Value = "3" });
            }

                @Html.DropDownListFor(model=>model.FirstOrDefault().RoomType
,RlistItems,RlistItems[item.RoomType.Value].Selected=true )
        </td>

娇纵 2024-11-07 05:04:38
<select asp-for="CountryName" asp-items=@(new List<SelectListItem> { new SelectListItem {Text="India",Value="1" } ,new SelectListItem {Text="Japan",Value="2" }} ) class="form-control">

<option>SELECT COUNTRY -- </option>

<select asp-for="CountryName" asp-items=@(new List<SelectListItem> { new SelectListItem {Text="India",Value="1" } ,new SelectListItem {Text="Japan",Value="2" }} ) class="form-control">

<option>SELECT COUNTRY -- </option>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文