具有键&#39; User.RoleID&#39;的ViewData项。属于&#39; System.Int32&#39;但必须属于&#39; IEnumerable <selectlistitem>&#39;

时间:2016-01-22 07:31:44

标签: c# model-view-controller

当我执行以下代码时,我收到此错误。我无法解决这个问题。有谁能够帮我?任何帮助将不胜感激。

型号:

public class UserViewModel
    {
        public Users User { get; set; }
        public List<ExamGroups> AllGroups { get; set; }
        public List<ExamGroups> UserGroups { get; set; }
        //public List<> SelectedExamGroups { get; set; }
        public int[] SelectedGroups { get; set; }

    }

查看:

        <div class="editor-label">
            @Html.LabelFor(model => model.User.RoleID)
        </div>
        <div class="editor-field">
            @Html.DropDownListFor(model => model.User.RoleID, (SelectList)ViewBag.UserRoles, "Seçiniz")
            @Html.ValidationMessageFor(model => model.User.RoleID)
        </div>

控制器:

public ActionResult Create() { 
    ViewBag.UserRoles = new SelectList(db.ListItem.Where(a => a.ListID == 2), "ListValue", "ListText");
    var q = new SelectList(new[]{ new { ID= "1", Name="Aktif"}, new { ID= "0", Name="Pasif"}, } ,"ID","Name",1);
    ViewBag.Status = q; 
    return View(InitilizeData());
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(UserViewModel users)
{
    if (ModelState.IsValid)
    {
        string IDs = "";
        for (int i = 0; i < users.SelectedGroups.Count(); i++)
        {
            IDs += users.SelectedGroups[i].ToString() + "|";
        }
        db.CreateUser(users.User.RegistryNo, users.User.Name, users.User.Surname, users.User.Password, users.User.RoleID, users.User.IsActive, IDs, users.User.LastLoginDate);
    } 
    return View();
}

0 个答案:

没有答案