2014-05-09 14 views
3

我想創建一個下拉列表,但它給了我一個錯誤,說'不能隱式轉換類型'字符串'System.Web.Mvc.SelectList'。我的代碼如下:在MVC5中創建一個下拉列表

應用數據庫模型:

public string dropdown{ get; set; } 

應用視圖模型:

public SelectList dropdown{ get; set; } 

ApplicationService.cs:

public static SelectList GetDropdownList(string currSelection) 
    { 
     List<SelectListItem> list = new List<SelectListItem>(); 
     list.Add(new SelectListItem { Value = "1", Text = "firstvalue" }); 
     list.Add(new SelectListItem { Value = "2", Text = "secondvalure" }); 
     list.Add(new SelectListItem { Value = "3", Text = "All of the Above" }); 


     return new SelectList(list, "Value", "Text", currSelection); 
    } 

在我的控制器我打電話:

applicationviewmodel.dropdown= ApplicationService.GetDropdownList(null); 

and then trying to save it in database as: 

ApplicationDatabaseModel.dropdown= applicationviewmodel.dropdown; 

這是我得到這個錯誤的地方。

在我看來,我有:

@Html.DropDownListFor(x => x.dropdown, applicationviewmodel.dropdown) 

我不知道如何使這項工作。

+0

'ApplicationDatabaseModel.dropdown'是什麼類型? '串'? –

+0

是的。它是從數據庫創建的模型 – user3541362

+0

好吧,'ApplicationDatabaseModel.dropdown = applicationviewmodel.dropdown'說「請將'SelectList'對象分配給'string'」。這是你的問題所在。你如何建議在'string'中存儲整個'SelectList'實例?你需要考慮如何做到這一點..編譯器不會爲你做。 –

回答

0

此:

@Html.DropDownListFor(x => x.dropdown, applicationviewmodel.dropdown) 

..是不正確。它試圖將選擇的項目存儲到SelectList實例中。

你想要的是在視圖模型string變量,這個值被選進:

public class ApplicationViewModel { 
    public SelectList DropDown { get; set; } 
    public string SelectedDropDownValue { get; set; } 
    // .. the rest of the properties here 
} 

那麼你的看法變成這樣:

@Html.DropDownListFor(x => x.SelectedDropDownValue, Model.DropDown) 

這是說「選擇的值存儲到SelectedDropDownValue「。

然後,您需要更改如何構建SelectListValue是發送到您的財產.. Text是什麼顯示在瀏覽器中。

所以這個:

list.Add(new SelectListItem { Value = "1", Text = "firstvalue" }); 
list.Add(new SelectListItem { Value = "2", Text = "secondvalure" }); 
list.Add(new SelectListItem { Value = "3", Text = "All of the Above" }); 

..has是這樣的:

list.Add(new SelectListItem { Value = "firstvalue", Text = "firstvalue" }); 
list.Add(new SelectListItem { Value = "secondvalue", Text = "secondvalure" }); 
list.Add(new SelectListItem { Value = "all of the above", Text = "All of the Above" }); 

..because他們是字符串(當然,除非你想貼背面的數字) 。

然後,最後,你的控制器代碼變成這樣:

// assign the string value to the string property 
ApplicationDatabaseModel.dropdown = applicationviewmodel.SelectedDropDownValue; 
+0

我嘗試過,但它給了我一個錯誤'沒有ViewData項的類型'IEnumerable '有關鍵'model.selecteddropdownvalue' – user3541362

2

我覺得它更容易只是有一個列表作爲模型的一部分,並使用一個簡單的LINQ語句。以下爲國家簡單的例子下拉:

假設你有一個像

public class MyModel() 
{ 
    public int CountryId { get; set; } 
    public List<Country> Countries { get; set; } 
} 

模型,並在您的視圖中的國家類的

public class Country() 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
} 

那麼你就可以做到以下幾點:

@Html.DropDownListFor(m => m.CountryId, 
          Model.Countries.Select(x => 
           new SelectListItem { Text = x.Name, Value = x.Id.ToString(), Selected = Model.CountryId == x.Id }, "Please Select...", null)