4

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

任何帮助将不胜感激。

提前致谢。

4

2 回答 2

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)
于 2013-03-28T17:19:51.970 回答
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">
于 2013-03-28T17:43:26.860 回答