MVC编辑对象不起作用

时间:2014-05-29 08:43:03

标签: c# entity-framework model-view-controller

我使用的是Entity Framework,我有两个类:

    public class User
    {
            public int UserID { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string Email { get; set; }
            public string UserName { get; set; }
            public string Password { get; set; }
            public int PermissionGroupID { get; set; }
            public string Comments { get; set; }

            public virtual PermissionGroup PermissionGroup { get; set; }
    }

public class PermissionGroup
{
        public int PermissionID { get; set; }
        public string PermissionValue { get; set; }
}

当然,在我的db中,我在User.PermissionGroupID和PermissionGroup.PermissionID之间有FK

现在,我尝试使用UserModel进行编辑控制器和查看:

public class UserModel
{
    public User User { get; set; }
    public SelectList PermissionGroups { get; set; }
    public int SelectedPermissionGroup { get; set; }
}

与控制器:

   public ActionResult Edit(int id = 0)
    {
        UserModel model = new UserModel ();
        model.User= managerUser.GetObjectById(id);
        model.PermissionGroups = new SelectList(managerPermission.GetAllList(), "PermissionID ", "PermissionValue");

        if (model.User== null)
        {
            return HttpNotFound();
        }

        model.SelectedPermissionGroup = model.User.PermissionGroupID;

        return View(model);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(UserModel model)
    {
        if (ModelState.IsValid)
        {
            model.User.PermissionGroupID = model.SelectedPermissionGroup;
            managerUser.EditObject(model.User);
            return RedirectToAction("Index");
        }

        UserModel newModel = model;
        newModel.User = managerUser.GetObjectById(model.UserID);
        newModel.PermissionGroups = new SelectList(managerPermission.GetAllList(), "PermissionID ", "PermissionValue");

        return View(newModel);
    }

管理器中的EditObject是:

public virtual int EditObject(T objToEdit)
{
    DbEntities.Set<T>().Attach(objToEdit);
    DbEntities.Entry(objToEdit).State = EntityState.Modified;
    return DbEntities.SaveChanges();
}

在我看来,我不会放任何东西,这就是我所拥有的:

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

    <fieldset>
        <legend>User</legend>

        @Html.HiddenFor(model => model.User.UserID)

//Everything else to edit about the user....

        <div class="form-group">
            @Html.LabelFor(model => model.User.PermissionGroupID)
            @Html.DropDownListFor(model => model.SelectedPermissionGroup, Model.PermissionGroups, new { @class = "form-control" })
            <p class="help-block">@Html.ValidationMessageFor(model => model.User.PermissionGroupID)</p>
        </div>

        <p>
            <input type="submit" value="Save" />
            @Html.ActionLink("Back", "Index")
        </p>
    </fieldset>
}

因此,当我单击&#34; Save&#34;时,根据我的断点,它正确传递所有内容,但是User类中的PermissionGroup属性未填充为PermissionGroupID(它确实从所选下拉列表中获取ID)。 然后它返回到索引视图,但没有进行任何更改。

我在其他项目中使用了完全相同的EditObject方法,它运行正常,包含dropdownlist和所有项目。

我错过了什么?

1 个答案:

答案 0 :(得分:0)

我怀疑这是错误,您正在验证Model.User.PermissionGroupID但是您已将Model.SelectedPermissionGroup

绑定到下拉列表
@Html.DropDownListFor(model => model.SelectedPermissionGroup, 
                      Model.PermissionGroups, 
                      new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.User.PermissionGroupID)

它应该是这样的:

@Html.DropDownListFor(model => model.User.PermissionGroupID, 
                               Model.PermissionGroups, 
                               new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.User.PermissionGroupID)