具有键'name'的viewdata项的类型为'system.string',但必须是'ienumerable< selectlistitem>'类型。 [英] The viewdata item that has the key 'name' is of type 'system.string' but must be of type 'ienumerable<selectlistitem>'.

查看:60
本文介绍了具有键'name'的viewdata项的类型为'system.string',但必须是'ienumerable< selectlistitem>'类型。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

// GET: /Account/Register
[AllowAnonymous]
public ActionResult Register()
{
    ViewBag.Name = new SelectList(context.Roles.ToList(), "Name", "Name");
    return View();
}





Register.cshtml

----------- ------



Register.cshtml
-----------------

<!--Select the Role Type for the User-->
    <div class="form-group">
        @Html.Label("Select Your User Type", new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @*@Html.DropDownList("Name")*@
    @*@Html.DropDownList("Name", (SelectList)ViewBag.Name, "--Choose Role--")*@
            @Html.DropDownList("Name", (SelectList)ViewBag.Name, "--Choose Role--")
            
        </div>
    </div>
    <!--Ends Here-->





我尝试过:





What I have tried:

The ViewData item that has the key 'Name' is of type 'System.String' but must be of type 'IEnumerable<SelectListItem>'.

推荐答案

参考此并将其修复为您的代码



控制器

refer this and fix it in your code

Controller
public class Role
   {
       public string Name { get; set; }
   }


   public class HomeController : Controller
   {
       public ActionResult Index()
       {
           List<Role> lst = new List<Role>();
           lst.Add(new Role() { Name = "Admin" });
           lst.Add(new Role() { Name = "Master" });
           lst.Add(new Role() { Name = "user" });
           ViewBag.Name = new SelectList(lst, "Name", "Name");
           return View();
       }

   }



查看




View

@Html.DropDownList("Name", (SelectList)ViewBag.Name, "--Choose Role--")


这篇关于具有键'name'的viewdata项的类型为'system.string',但必须是'ienumerable&lt; selectlistitem&gt;'类型。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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