2013-12-17 73 views
0

返回一個列表我有,我有我的看法名單,但這個名單是在所有的意見,多視圖和列表共享重複使用相同的信息MVC3如何從控制器

// MyView.cshtml 
@Html.DropDownListFor(model => model.type, new SelectList(new List<Object>{ 
       new { Text = "Automotive", Value = "Automotive"}, 
new { Text ="Business", Value= "Business"}}}, "Value", "Text")) 

這是下拉列表中的問題是,該列表有時會發生變化,因此我決定在控制器中創建這個列表,如果我需要改變一些東西,我可以在一個地方做它,它無論如何改變了一切,我想知道我怎麼能從控制器使用不同的actionResult使列表顯示在MyView.cshtml

public ActionResult articleList() 
    { 
     var mylist = new List<articledrop> 
     { 
      new articledrop{ Text = "Automotive", Value = "Automotive"}, 
      new articledrop{ Text ="Business", Value= "Business"} 

     }; 
     return View(mylist); 
    } 

,你可以看到我已經在視圖中創建的列表articleList.cshtml現在我怎樣才能使使用下拉列表上MyView.cshtml顯示該清單..任何建議將是巨大

回答

0

你可以只添加包含列表中的靜態類,像這樣:

public static class UtilLists 
{ 
    public static List<articledrop> mylist = new List<articledrop> 
    { 
     new articledrop{ Text = "Automotive", Value = "Automotive"}, 
     new articledrop{ Text ="Business", Value= "Business"} 
    }; 
} 

而在查看你只需要調用它是這樣的:

@Html.DropDownListFor(model => model.type, 
         MvcApplication1.UtilLists.mylist.Select(option => new SelectListItem { 
           Text = option.Text, 
           Value = option.Value}), 
        "Choose..") 

讓我知道;)