编辑角色中的InvalidOperationException

时间:2015-01-20 07:41:00

标签: c# razor model-view-controller

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

  

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

类型

这些是我

的代码
    GET and POST Edit ` //
    // 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);
        }
    }`

这是编辑视图

    @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>
        }

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

0 个答案:

没有答案