2

My code is like this Model

public class Department
{
    public int DepartmentID { get; set; }
    public string Name { get; set; }
    public bool Status { get; set; } //no appointments when false
    public DateTime CreatedDate { get; set; }
    public int CreatedUserId
    {
        get { return ((SystemUser) HttpContext.Current.Session["CurrentUser"]).SystemUserID; }
    }

}

Controller

 [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include="DepartmentID,Name,CreatedUser,CreatedDate")] Department department)
    {
        if (ModelState.IsValid)
        {
            db.Entry(department).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(department);
    }

View (Edit)

    @model DiagnosisApp.Models.Department

@using (Html.BeginForm())
{
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)
        @Html.HiddenFor(model => model.DepartmentID)
        @Html.TextBoxFor(model => model.Name, new { @class = "form-control", @placeholder = "Enter Department Name" })
        <button type="submit" value="Save" >Save</button>

}

As you can see in view I only want to Update the Name field , But when I run the code all fields in table are updated.For which columns I have not added value gets updated with null. Can anyone point out what I am doing wrong here?

None
  • 5,582
  • 21
  • 85
  • 170
  • its because you UI model dont have all the fields rendered. So for those properties which is not used in the UI model binding sending null values – qamar Jan 21 '15 at 10:38
  • Get the existing item from the database, update its `Name` based on the posted values and save the original. Note you can remove `CreatedUser, CreatedDate` from the `[Bind(Include...)]` –  Jan 21 '15 at 10:38
  • Is it a good practice to render unwanted fields in UI? Is there no other way to overcome this? May be inside Controller Logic? – None Jan 21 '15 at 10:39
  • No its not. I am posting an answer soon to update only one property – qamar Jan 21 '15 at 10:40
  • @StephenMuecke I haven't completely understood what you say. Can you please add it as an answer ? – None Jan 21 '15 at 10:40
  • @qamar Okay, thats great – None Jan 21 '15 at 10:40
  • No its not good practice. Its preferable to use a view model with only the properties you want to display/edit [refer What is a view model in MVC](http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) –  Jan 21 '15 at 10:40

1 Answers1

2

I think what you have done wrong is forgot to attach the entity to the context. And of course model object don't have all the properties. So its model binder sending default values for those properties which is not rendered in the UI.

           if (ModelState.IsValid)
            {
                db.Department.Attach(department);
                db.Entry(department).Property(x => x.name).IsModified = true;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
qamar
  • 1,437
  • 1
  • 9
  • 12