2012-04-03 74 views
2

首先,我是ASP.NET MVC的新手。現在我正試圖在C#Razor視圖中添加下拉列表。 問題是,有無論如何直接將List()分配給@ Html.DropDownListFor ...? MyClass的是這樣的MVC3中的DropDownListFor

public class MyClass 
{ 
    public int ID {get; set;} 
    public string Itemstring {get; set;} 
} 

視圖模型是這樣的

public class MyViewModel 
{ 
    public int ID { get; set; } 
    public List<MyClass> MyClassList {get; set; } 
} 

控制器的操作方法是這樣的

public ActionResult Home() 
{ 
MyViewModel mvm = new MyViewModel() { 
new MyClass() { ID=1, Itemstring="My Item1" }, 
new MyClass() { ID=2, Itemstring="My Item2" }, 
new MyClass() { ID=3, Itemstring="My Item3" } 
} 
} 

上觀看:這是我不知道如何使用

@model xyzNameSpace.Models.MyViewModel 
<div> 
@Html.DropDownListFor(x => x.ID, Model.MyClassList); 
<a href="@Url.Action("Selected")><img src="../../Content/images/selected.gif" alt="Proceed" /> </a> 
</div> 

- 因此,當用戶從下拉列表中選擇一個項目並單擊圖像時,它應該調用(或調用不確定要使用哪個單詞)'Selected'操作,並且它應該將模型與選定值綁定,幫助我如何做到這一點...?

謝謝 Siri。

+0

所以,你要選擇的項目從下拉列表上傳到服務器時, 「繼續」按鈕被點擊? – 2012-04-03 18:44:24

回答

4

Html.DropDownListFor有6點過載,他們都採取IEnumerable<SelectListItem>作爲將持有你的價值觀的參數。你不能直接使用你自己的類,這就是爲什麼當你嘗試Kyle的解決方案時你會得到這個錯誤。

添加到您的模型:

public List<SelectListItem> ListToUse { get; set; } 

控制器:

MyViewModel model = new MyViewModel(); 
model.MyClassList = new List<MyClass> 
{ 
    new MyClass() { ID=1, Itemstring="My Item1" }, 
    new MyClass() { ID=2, Itemstring="My Item2" }, 
    new MyClass() { ID=3, Itemstring="My Item3" } 
} 
model.ListToUse = model.MyClassList 
    .Select(x => new SelectListItem{ Value = x.ID, Text = x.Itemstring }) 
    .ToList(); 

在視圖:

@Html.DropDownListFor(x => x.ID, Model.ListToUse); 

我沒有測試此代碼。

+0

酷似看起來像這樣工作,但我會回到'所選'行動參數...? – siri 2012-04-03 20:00:22

+0

我的意思是將這是一個'MyViewModel'本身或只是選定的ID作爲Int ...? – siri 2012-04-03 20:07:23

+0

不知道我明白這個問題。當你發佈你的表單時,'model.ID'將填充選定的值。 – Dmitry 2012-04-03 20:07:40

0

如果我正確理解你,當你點擊「繼續」按鈕時,你想從下拉列表中將所選項目發佈到服務器上?

您將需要使用窗體來做到這一點:

@model xyzNameSpace.Models.MyViewModel 
@using(Html.BeginForm()) 
{ 
    <div> 
    @Html.DropDownListFor(x => x.ID, Model.MyClassList); 

    <input type="submit" value="Proceed" /> 
    </div> 
} 
+0

是的,這就是我想要做的。但是當我編譯你的代碼時會拋出以下錯誤。 – siri 2012-04-03 19:01:53

+0

..... MyViewModel不包含'DropDownListFor'的定義和最佳擴展方法重載。 – siri 2012-04-03 19:03:00

+0

對不起,這裏是確切的錯誤消息:錯誤CS1928: 「System.Web.Mvc.HtmlHelper 」不包含關於「DropDownListFor」 的定義和最佳的擴展方法過載「的System.Web .Mvc.Html.SelectExtensions。DropDownListFor (System.Web.Mvc.HtmlHelper ,System.Linq.Expressions.Expression >, System.Collections.Generic.IEnumerable )'有一些無效參數 – siri 2012-04-03 19:06:16

0

模型

public class modelname 
      { 
       public selectlist getdropdown() 
       { 
        IEnumerable<SelectListItem> EntityList = edmx.tblEntities.AsEnumerable().Select(a => new SelectListItem { Text = a.name, Value = a.id.ToString() }).ToList(); 
        return new SelectList(EntityList, "Value", "Text");    

} 
     } 

鑑於

@Html.DropDownListFor(model => model.source_entity, Model.getdropdown(), "--Select--") 

中位指示

public ActionResult home() 
     { 
      return View(new modelname()); 
     }