2013-03-28 25 views
4

我是ASP.NET MVC的新手,我很感謝任何關於我的問題的幫助。我已經在這個話題上做了大量的研究(顯然還不夠)。我需要將一個下拉列表綁定到表中的特定列,然後將其呈現在視圖中。我已經有查詢檢索控制器表:將視圖中的DropDownList綁定到使用ASP.NET MVC4的表中​​的列

public ActionResult SelectAccountEmail() 
     { 
      var queryAccountEmail = (from AccountEmail in db.UserBases select AccountEmail) 
      var selectItems = new SelectList(queryAccountEmail); 
      return View(selectItems); 
     } 

的時候纔來查詢綁定到視圖中的下拉列表我迷路。

Server Error in '/' Application. 
-------------------------------------------------------------------------------- 


The model item passed into the dictionary is of type 'System.Web.Mvc.SelectList', but this dictionary requires a model item of type 'RecordUploaderMVC4.Models.UserBase'. 

任何幫助將不勝感激:

@model RecordUploaderMVC4.Models.UserBase 

@{ 
    ViewBag.Title = "SelectAccountEmail"; 
} 

<h2>SelectAccountEmail</h2> 

@Html.LabelFor(model => model.AccountEmail); 

@Html.DropDownList(Model.AccountEmail); 
@Html.ValidationMessageFor(model => model.AccountEmail); 

<input /type="submit" value="Submit"> 

當我運行它,我得到這個錯誤。

在此先感謝。

+0

視圖是期待RecordUplo aderMVC4.Models.UserBase作爲模型,並傳遞一個SelectList類型的對象。提供適當類型的模型對象。 – eka

回答

3

幾件事情不對。首先,改變你的模型中添加以下屬性(在視圖來看,它的RecordUploaderMVC4.Models.UserBase):

public class UserBase 
{ 
    public string AccountEmail { get; set; } 
    public SelectList Emails { get; set; } 
    //rest of your model 
} 

然後,在你的控制器建立模型正確:

public ActionResult SelectAccountEmail() 
{ 
    UserBase model = new UserBase(); 
    var queryAccountEmail = (from AccountEmail in db.UserBases select AccountEmail) 
    model.Emails = new SelectList(queryAccountEmail); 

    return View(model); 
} 
在你看來,你可以

然後這樣做:

@Html.LabelFor(model => model.AccountEmail) 

@Html.DropDownListFor(model => model.AccountEmail, Model.Emails) 
@Html.ValidationMessageFor(model => model.AccountEmail) 
+0

感謝您的快速反應mattytommo! 我實際上使用的是實體框架5,並且首先對我正在使用的數據庫進行反向工程代碼,然後添加了一個ADO.NET實體數據模型,以便我可以使用存儲過程作爲反向工程代碼提供sprocs的使用。長話短說,UserBase模型是由實體框架生成的。因此,你是否建議通過添加公共SelectList電子郵件行來修改它? – Oliver

+1

我希望這種類型的示例將成爲MS的每個MVC教程的第一部分。不知道爲什麼有太多的重點(可怕的)ViewData/Bag的東西,並在控制器做所有的工作。 – Graham

0

第1步: 首先創建一個模型,像這樣牽着你的ListofAccountEmail

public class AccountEmailViewModel 
    { 

     public int AccountEmailId { get; set; } 
     public string AccountEmailDescription { get; set; } 
    } 

步驟2:創建模型類

public class UserBaseViewModel 
    {  


       public IEnumerable<SelectListItem> AccountEmail { get; set; } 
       public string AccountEmail { get; set; } 
    } 

步驟3:

在控制器

  [HttppGet] 
      public ActionResult SelectAccountEmail() 
      { 
       var EmailAccounts = (from AccountEmail in db.UserBases select AccountEmail) 

       UserBase userbaseViewModel = new UserBase 
       { 
        AccountEmail = EmailAccounts.Select(x => new SelectListItem 
        { 
         Text = x.AccountEmailDescription, 
         Value = Convert.ToString(x.AccountEmailId) 
        }).ToList() 

       }; 
       return View(userbaseViewModel); 
      } 

步驟4:在查看

@model RecordUploaderMVC4.Models.UserBase 
    @{ 
     ViewBag.Title = "SelectAccountEmail"; 
    } 
    <h2>SelectAccountEmail</h2> 
    @Html.ValidationSummary() 
    <h2>SelectAccountEmail</h2> 
          @Html.LabelFor(model => model.AccountEmail) 

           @Html.DropDownListFor(x => x.AccountEmailId, Model.AccountEmail, "Please Select", "") 
          </div> 
    <input /type="submit" value="Submit"> 
相關問題