使用ASP.NET MVC4将视图中的DropDownList绑定到表中的列

时间:2013-03-28 17:14:29

标签: c# asp.net-mvc razor

我是ASP.NET MVC的新手,对于我的问题,我将不胜感激。我已经就这个主题进行了大量的研究(显然还不够)。我需要将下拉列表绑定到表中的特定列,然后在视图中呈现它。我已经有了查询来检索控制器中的表:

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

将查询绑定到视图中的下拉列表时,我迷路了。

@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">

运行时出现此错误:

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'. 

任何帮助将不胜感激。

提前致谢。

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分: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">