简体   繁体   English

MVC编辑物件无法运作

[英]MVC Edit object doesn't work

I'm using Entity Framework, and I have 2 classes: 我正在使用Entity Framework,并且有2个类:

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

of course, in my db I have FK between User.PermissionGroupID and PermissionGroup.PermissionID 当然,在我的数据库中,我在User.PermissionGroupID和PermissionGroup.PermissionID之间有FK

Now, I'm trying to do the edit controller and view using a UserModel: 现在,我尝试使用用户模型来执行编辑控制器并进行查看:

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

with the controller: 与控制器:

   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);
    }

The EditObject in the manager is this: 管理器中的EditObject是这样的:

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

And in my view I'll not put evrything, this is what I have: 在我看来,我不会提出任何疑问,这就是我所拥有的:

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

So, When I click "Save", according to my breakpoints, it passes everything correctly but the PermissionGroup property in User class is not populated as the PermissionGroupID (it does recieve the ID from the selected dropdown. And then it returns to the Index view but without any of the changes I made. 因此,当我单击“保存”时,根据我的断点,它会正确传递所有内容,但不会将User类中的PermissionGroup属性填充为PermissionGroupID(它确实从选定的下拉列表中接收ID。然后返回到索引视图)但没有进行任何更改。

I used the exact same EditObject method in other projects and it worked fine, with dropdownlist and all. 我在其他项目中使用了完全相同的EditObject方法,并且在dropdownlist和all上都可以正常工作。

What am I missing? 我想念什么?

i suspect that here is the mistake, you are validating for Model.User.PermissionGroupID but you have binded dropdown list with Model.SelectedPermissionGroup 我怀疑这是错误,您正在验证Model.User.PermissionGroupID但您已将下拉列表与Model.SelectedPermissionGroup

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

it should be this: 应该是这样的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM