如何填补MVC4剃刀意见的DropDownList(C#) [英] How to fill dropdownlist for MVC4 razor views (C#)

查看:141
本文介绍了如何填补MVC4剃刀意见的DropDownList(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示范的UserProfiles

  [表(用户)]
公共类的UserProfiles
{
    [键]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)
    公众诠释用户ID {搞定;组; }
    公共字符串用户名{获得;组; }
    //公共字符串密码{搞定;组; }    公共字符串名字{获得;组; }
    公共字符串名字{获得;组; }
    公共字符串中间名{获得;组; }
    公共字符串缩写{搞定;组; }    公共字符串公司{搞定;组; }
    公共字符串系{搞定;组; }
    公共字符串名称{搞定;组; }
    公共字符串小组{搞定;组; }
    公共字符串TeamSub {搞定;组; }
    公共字符串电话{搞定;组; }    公共字符串ImageLocation {搞定;组; }    公共字符串注意{搞定;组; }
    公共字符串评论{搞定;组; }}

控制器

 公众的ActionResult指数()
    {
        **编辑:** ViewBag.ProfileId =新的SelectList(db.UserProfiles,用户名,用户名);
        返回查看(db.UserProfiles.ToList());
    }

在View(我希望/相信这里是问题)

  @model IEnumerable的< OilNGasWeb.Models.UserProfiles>@ {
    ViewBag.Title =CLS - 石油和放大器;燃气网站用户;
}< H2>网站用户< / H>
    ** **删除@ Html.DropDownList(型号=> Model.UserName,Model.UserName)
    ** ** Changedinto @ Html.DropDownList(用户名的String.Empty)

新的错误:

 异常详细信息:System.InvalidOperationException:有是没有项目的ViewData
键入'的IEnumerable< SelectListItem>'具有关键的用户名。


解决方案

添加的SelectList到你的 ViewBag 中的控制器:

  ViewBag.ProfileId =新的SelectList(db.UserProfiles,ID,用户名);

然后在您查看补充一点:

  @ Html.DropDownList(概要文件的String.Empty)

此外,你应该在你的lambda前pression用来代替模范模范。

UserProfiles Model

[Table("Users")]
public class UserProfiles
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    public string UserName { get; set; }
    //public string Password { get; set; }

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string MiddleName { get; set; }
    public string Initials { get; set; }

    public string Company { get; set; }
    public string Department { get; set; }
    public string Title { get; set; }
    public string Team { get; set; }
    public string TeamSub { get; set; }
    public string Phone { get; set; }

    public string ImageLocation { get; set; }

    public string Note { get; set; }
    public string Comment { get; set; }

}

Controller

     public ActionResult Index()
    {
        **EDIT :** ViewBag.ProfileId = new SelectList(db.UserProfiles, "UserName", "UserName");
        return View(db.UserProfiles.ToList());
    }

In the View ( i hope / believe here is the issue )

@model IEnumerable<OilNGasWeb.Models.UserProfiles>

@{
    ViewBag.Title = "CLS - Oil & Gas Web Site Users";
}

<h2>Web Site Users</h2>


    **Removed** @Html.DropDownList(Model => Model.UserName,Model.UserName)
    **Changedinto** @Html.DropDownList("UserName", String.Empty)

New Error:

Exception Details: System.InvalidOperationException: There is no ViewData item of 
type 'IEnumerable<SelectListItem>' that has the key 'UserName'.

解决方案

Add a SelectList to your ViewBag in the Controller:

ViewBag.ProfileId = new SelectList(db.UserProfiles, "Id", "Username");

then add this in your View:

@Html.DropDownList("ProfileId", String.Empty)

Moreover you should have used "model" instead of "Model" in your lambda expression.

这篇关于如何填补MVC4剃刀意见的DropDownList(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆