Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 键为“key”的ViewData项是“System.Int32”,但必须是“IEnumerable类型”_Asp.net Mvc_Html.dropdownlistfor - Fatal编程技术网

Asp.net mvc 键为“key”的ViewData项是“System.Int32”,但必须是“IEnumerable类型”

Asp.net mvc 键为“key”的ViewData项是“System.Int32”,但必须是“IEnumerable类型”,asp.net-mvc,html.dropdownlistfor,Asp.net Mvc,Html.dropdownlistfor,具有key roleID的ViewData项是System.Int32,但必须为“IEnumerable”类型 我修不好 我需要你的帮助!!这是我的代码: 管理模式: [Key] public int adminID { get; set; } [DisplayName("Role")] public int roleID { get; set; } public virtual Role roles { get; set; } [Required(

具有key roleID的ViewData项是System.Int32,但必须为“IEnumerable”类型

我修不好 我需要你的帮助!!这是我的代码:

管理模式:

 [Key]
    public int adminID { get; set; }

    [DisplayName("Role")]
    public int roleID { get; set; }
    public virtual Role roles { get; set; }

    [Required(ErrorMessage ="Le nom est obligatoire.")]
    public string Nom { get; set; }

    [Required(ErrorMessage = "Le prenom est obligatoire.")]
    public string Prenom { get; set; }

    [Required(ErrorMessage = "L'email est obligatoire.")]
    [RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", ErrorMessage = "email n'est pas valide.")]
    [DataType(DataType.EmailAddress)]
    [Remote("IsEmailAdminExist", "Admin", ErrorMessage = "URL exist!")]
    public virtual  string Email { get; set; }

    [Required(ErrorMessage = "Le mot de pass est obligatoire.")]
    [DataType(DataType.Password)]
    public string Pass { get; set; }

    public string Photo { get; set; }

    [NotMapped]
    public HttpPostedFileBase PhotoUpload{ get; set; }
榜样:

[Key]
public int roleID { get; set; }

[Required(ErrorMessage = "Le nom est obligatoire.")]
public string Nom { get; set; }

public virtual ICollection<Admin> Admins { get; set; }
职位:

编辑视图:

                        <div class="form-group">
                            <label class="control-label">
                                Role <span class="symbol required"></span>
                            </label>
                            @Html.DropDownListFor(m => m.roleID, ViewBag.Roles as SelectList, "Selectioner un role", new { @class = "form-control search-select", id = "roleID" })
                        </div>

问题是在POST操作中没有重新填充SelectList,因此视图将返回给用户,而不包含SelectList

在POST action中重复此代码,或将其包装在干燥功能中:

var data = from p in AdminContext.roles
            select new
            {
                roleID = p.roleID,
                roleNom = p.Nom
            };

SelectList list = new SelectList(data, "roleID", "roleNom");
ViewBag.Roles = list;

你在controller中的POST操作是什么?我把它作为一个答案。非常感谢,问题是solvedplz如何在索引视图中获取角色的名称,因为我可以只获取角色Id。这是一个单独的问题。制作一个助手方法,将Id转换为名称,并在视图中使用它。或者制作另一个模型,使用字符串成员作为角色名称d使用lync从控制器中填充。感谢您的响应
                        <div class="form-group">
                            <label class="control-label">
                                Role <span class="symbol required"></span>
                            </label>
                            @Html.DropDownListFor(m => m.roleID, ViewBag.Roles as SelectList, "Selectioner un role", new { @class = "form-control search-select", id = "roleID" })
                        </div>
var data = from p in AdminContext.roles
            select new
            {
                roleID = p.roleID,
                roleNom = p.Nom
            };

SelectList list = new SelectList(data, "roleID", "roleNom");
ViewBag.Roles = list;