ASP.NET MVC Details view with many display fields but a single editable field

asp.net-mvc asp.net-mvc-viewmodel entity-framework-6 repository-pattern

Question

I have a view that shows a single item with all of its fields. But I'm getting confused trying to figure out how to allow one specific field ("Status") to be updated from this view, without necessarily going into a whole other "edit" view first.

How do I http-post to the same action (Details) but saving only the "Status" field, without saving all of the other properties which are part of the view model (for display only)? Do I need a separate view model just for the Status? New to ASP.NET MVC and getting confused.

View Model

public string FirstName { get; set; }
public string LastName { get; set; }
public string Birthdate{ get; set; }
public string Status { get; set; }
//etc.

View

<div>
    @Html.DisplayFor(model => model.FirstName)
    @Html.DisplayFor(model => model.LastName)
    @Html.DisplayFor(model => model.Birthdate)
    //etc.
</div>
<div>
    @TextBoxFor(model => model.Status)
    <button type="submit" value="Save Status" />
</div>

Controller

[HttpGet]
public ActionResult Details(int id)
{
    var person = personRepo.GetById(id);
    var vm = BuildPersonDetailsViewModel(person);
    return View(vm);
}

[HttpPost]
public ActionResult Details(PersonDetailsViewModel vm)
{
    var person = personRepo.GetById(vm.PersonID);
    person.Status = vm.Status;
    personRepo.Update(person);
}
1
1
3/7/2015 9:03:30 PM

Accepted Answer

So I solved this by ensuring that the primary key field PersonID is included in the View. I didn't think I needed it originally because it started off as a read-only Details view, and PersonID wasn't needed to be displayed. But when posting data back, I needed to add it as a hidden field, so it could be passed to the controller on HttpPost. Then it can be used to locate and update the record in the database.

Furthermore, I added another method in the repository to allow for updating of just the "Status" field, since that's the only value being updated. If I use the sample code above, my solution looks something like this:

View Model

public int PersonID { get; set; }
public string Status { get; set; }
public string FirstName { get; set; }
//etc.

View

@Html.HiddenFor(model => model.PersonID)

@Html.DisplayFor(model => model.FirstName)
//etc. (all the display fields)

@TextBoxFor(model => model.Status)
<button type="submit" value="Save Status" />

Controller

[HttpPost]
public ActionResult Details(PersonDetailsViewModel vm)
{
    personRepo.UpdateStatus(vm.PersonID, vm.Status);
}
2
3/10/2015 5:23:43 PM

Popular Answer

This is probably not the case but since i can't tell from the sample code, do you have the Html.BeginForm(){} block around the model that you are trying to post? Also maybe try changing the <button> tag to <input type='submit' value='save status'/> instead



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow