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

In my perspective, each item is displayed along with all of its fields. However, I'm having trouble figuring out how to enable one single field—Status—to be altered from this view without first entering an entirely different "edit" page.

How can I http-post to the same action (Details) while only preserving the "Status" field and leaving the rest of the view model's properties (which are purely for display) in tact? Do I require a unique view model for the Status alone? I'm new to ASP.NET MVC and I'm lost.

Watch 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 resolved this by making sure the View contains the primary key field PersonID. Since it was initially a read-only Details view and PersonID wasn't required to be displayed, I didn't first think I needed it. However, I had to include it as a hidden field when publishing data back so that it could be sent to the controller via HttpPost. The database record can then be found and updated using it.

The "Status" field is the only one that can be altered, so I have added another method to the repository to enable that. If I use the aforementioned sample code, my response appears somewhat like this:

Watch 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

Since I can't tell from the sample code, even if this is probably not the case, do you have theHtml.BeginForm(){} block surrounding the model you're attempting to post? could try altering 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