如何在mvc3 razor中使用DropListListFor的SelectListItem [英] How to use SelectListItem for DropdownListFor in mvc3 razor

查看:93
本文介绍了如何在mvc3 razor中使用DropListListFor的SelectListItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想在我的视图中使用dropdownbox下面的属性。

我怎么能这样?

我尝试了很多方法但是:(

请帮助





Hi I want to use below property in my view with dropdownbox.
How could I ?
I have tried so many ways but :(
Please help


public IEnumerable<SelectListItem> Topic
{
    get
    {
        return  new[]
    {
        new SelectListItem { Value = "1", Text = "Science", Selected=true },
        new SelectListItem { Value = "2", Text = "History" },
        new SelectListItem { Value = "3", Text = "Physics" },
    };
    }
}

推荐答案

试试这个

try this
public static class DropDownList<t>
   {
       public static SelectList LoadItems(IList<t> collection, string value, string text)
       {
           return new SelectList(collection, value, text);
       }
   }
Call the method from the controller like Below.
ViewData["Executives"] =
                DropDownList<executives>.LoadItems(
                    objExecutivesDbContext.Executives.ToList(), "ExecutiveId", "ExecutiveName ");
Call the viewData from the View like below
<div class="editor-field">
            @Html.DropDownListFor(model => model.ExecutiveId, (IEnumerable<selectlistitem>) ViewData["Executives"], "--Select--")



希望这会有所帮助


Hope this helps


这篇关于如何在mvc3 razor中使用DropListListFor的SelectListItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆