编辑用户角色时出现InvalidOperationException

时间:2015-01-27 06:37:37

标签: c# asp.net-mvc razor

每当我保存更改用户角色时所做的更改时,我就会遇到此问题:

  

System.Web.Mvc.dll中出现“System.InvalidOperationException”类型的异常,但未在用户代码中处理。额外   信息:具有键“URole”的ViewData项是类型   'System.Int32'但必须是'IEnumerable

类型

这些是我的GET和POST编辑的代码

// GET: /AccountAdmin/Edit/5

public ActionResult Edit(int id)
{

    Account account = db.Accounts.Find(id);
    if (account == null)
    {
        return HttpNotFound();
    }
    RoleDropDownList(account.URole);
    return View(account);
}

//
// POST: /AccountAdmin/Edit/5

[HttpPost]
public ActionResult Edit(Account account)
{
    try
    {

        if (ModelState.IsValid)
        {
            db.Entry(account).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        RoleDropDownList(account.URole);
        return View(account);
    }
    catch (Exception ex)
    {

        return View(account);
    }
}
 private void RoleDropDownList(object selectedRole = null)
    {
        var q = from a in db.Roles
                orderby a.RoleID
                select a;
        ViewBag.URole = new SelectList(q, "RoleID", "UserRole", selectedRole);
    }

这是编辑视图

 @model MvcApplication3.Models.Account

    @ViewBag.Title = "Edit";

    <h2>Edit</h2>

    @using (Html.BeginForm()) {
        @Html.ValidationSummary(true)

    <fieldset>
        <legend>Account</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.AccID)
        </div>
        <div class="editor-field">
            @Html.DisplayFor(model => model.AccID)
            @Html.ValidationMessageFor(model => model.AccID)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.UName)
        </div>
        <div class="editor-field">
            @Html.DisplayFor(model => model.UName)
            @Html.ValidationMessageFor(model => model.UName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Pword)
        </div>
        <div class="editor-field">
            @Html.DisplayFor(model => model.Pword)
            @Html.ValidationMessageFor(model => model.Pword)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.FName)
        </div>
        <div class="editor-field">
            @Html.DisplayFor(model => model.FName)
            @Html.ValidationMessageFor(model => model.FName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.LName)
        </div>
        <div class="editor-field">
            @Html.DisplayFor(model => model.LName)
            @Html.ValidationMessageFor(model => model.LName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.URole, "Role")
        </div>
        <div class="editor-field">
            @Html.DropDownList("URole", String.Empty)
            @Html.ValidationMessageFor(model => model.URole)
        </div>

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}

    <div>
        @Html.ActionLink("Back to List", "Index")
    </div>

        @section Scripts{
            <script src="@System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/bundles/jqueryval")"></script>
        }

可能是什么问题?提前谢谢!

1 个答案:

答案 0 :(得分:0)

问题出在这里。

<div class="editor-field">
    @Html.DropDownList("URole", String.Empty)
    @Html.ValidationMessageFor(model => model.URole)
</div>

此片段从模型属性获取URole。这可能是整数。所以在枚举int的同时给予期望。这是处理此问题的另一种方法。

public ActionResult Edit(int id)
{

   Account account = db.Accounts.Find(id);
   if (account == null)
   {
      return HttpNotFound();
   }
   RoleDropDownList(account.URole); // Assuming RoleDropDownList returns a SelectList
   return View(account);
}

然后在视野中。

@Html.DropDownList("URole",ViewBag.Roles as SelectList,string.Empty,  
 new { @name= "URole" });