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

问题描述


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

我怎么能这样?

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

请帮助





HiI 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" },
    };
    }
}

推荐答案

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 19:55