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

C# 使用ASP.NET MVC4将视图中的DropDownList绑定到表中的列,c#,asp.net-mvc,razor,C#,Asp.net Mvc,Razor,我是ASP.NET MVC的新手,如果您对我的问题有任何帮助,我将不胜感激。我已经在这个话题上做了大量的研究(显然还不够)。我需要将dropdownlist绑定到表中的特定列,然后在视图中呈现它。我已经有了在控制器中检索表的查询: public ActionResult SelectAccountEmail() { var queryAccountEmail = (from AccountEmail in db.UserBases select Acc

我是ASP.NET MVC的新手,如果您对我的问题有任何帮助,我将不胜感激。我已经在这个话题上做了大量的研究(显然还不够)。我需要将dropdownlist绑定到表中的特定列,然后在视图中呈现它。我已经有了在控制器中检索表的查询:

  public ActionResult SelectAccountEmail()
        {
            var queryAccountEmail = (from AccountEmail in db.UserBases select AccountEmail) 
            var selectItems = new SelectList(queryAccountEmail);
            return View(selectItems); 
        }
当将查询绑定到视图中的dropdownlist时,我会迷失方向

@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">
任何帮助都将不胜感激


提前谢谢

没什么不对的。首先,更改模型以添加以下属性(查看视图,它是
RecordUploaderMVC4.Models.UserBase
):

然后,在控制器中正确构建模型:

 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: 首先创建一个这样的模型来保存您的电子邮件列表

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; }
    }
步骤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">
@model RecordUploaderMVC4.Models.UserBase
@{
ViewBag.Title=“SelectAccountEmail”;
}
选择AccountEmail
@Html.ValidationSummary()
选择AccountEmail
@Html.LabelFor(model=>model.AccountEmail)
@Html.DropDownListFor(x=>x.AccountEmailId,Model.AccountEmail,“请选择”,“”)

感谢您的快速响应MattyMo!我实际上使用的是Entity Framework 5,我首先对我使用的数据库进行了反向工程编码,然后添加了ADO.NET实体数据模型,这样我就可以使用存储过程,因为反向工程代码首先没有提供存储过程的使用。长话短说,用户基础模型是由实体框架生成的。因此,您是否建议通过添加public SelectList Emails行来修改它?我希望这类示例将成为MS的每个MVC教程的第一部分。不确定为什么会如此关注(糟糕的)ViewData/Bag内容,在执行所有工作的控制器上。您的视图需要RecordUploaderMVC4.Models.UserBase作为模型,并且您正在传递SelectList类型的对象。提供适当类型的模型对象。
 public class UserBaseViewModel
    {    


                public IEnumerable<SelectListItem>  AccountEmail { get; set; }
                public string AccountEmail { get; set; }
    }
            [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);
            }
 @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">