简体   繁体   中英

MVC Edit object doesn't work

I'm using Entity Framework, and I have 2 classes:

    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

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:

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.

I used the exact same EditObject method in other projects and it worked fine, with dropdownlist and 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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