Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to round-trip view-only data between views and controllers
    primarykey
    data
    text
    <p>I'm passing data between my controller and my view using a ViewModel class. When there are validation errors, I return the ViewModel back to the view so the user can see the errors.</p> <p>I'm having trouble figuring out the best way to deal with the data that is only passed from the controller to the view, which isn't passed back to the controller, such as the contents of dropdown lists.</p> <p>Here's a simplified example from the project I'm working on:</p> <p>I have a <code>Widget</code> object in my domain model that has an <code>Employee</code> property. I have a view that allows the user to edit this employee property by selecting their name from a drop down list.</p> <pre><code>public class WidgetFormViewModel { // Used for a drop down list in the view public SelectList EmployeeList { get; set; } // This will contain the employee the user selected from the list public int EmployeeID { get; set; } public Widget Widget { get; set; } } </code></pre> <p>And the controller:</p> <pre><code>// GET: /Widget/Edit/1 public ActionResult Edit(int id) { var widget = _widgetService.GetWidgetByID(id); var employees = _widgetService.GetAllEmployees(); var viewModel = new WidgetFormViewModel() { EmployeeList = new SelectList(employees, "ID", "Name", widget.Employee), Widget = widget, WidgetID = widget.ID }; return View("Edit", viewModel); } // POST: /Widget/Edit public ActionResult Edit(WidgetFormViewModel viewModel) { var existingWidget = _widgetService.GetWidgetByWidgetID(viewModel.WidgetID); existingWidget.Employee = _widgetService.GetEmployeeByID(viewModel.EmployeeID); // try { /* Save widget to DB */ } catch { /* Validation errors */ } return ModelState.IsValid // Update was successful ? (ActionResult) RedirectToAction("List") // Model state is invalid, send the viewModel back to the view : View("Edit", viewModel) } </code></pre> <p>Now, here's the problem: When the <code>ModelState</code> is invalid and <code>viewModel</code> gets passed back to the view, its <code>EmployeeList</code> property is blank. What is the best way to deal with this? </p> <p>Should I just repopulate it before returning to the view? This method seems difficult to maintain. (What if I add <code>PageTitle</code> and <code>HeaderText</code> to the view model as well? Suddenly there are more things to keep track of.) Is there another approach?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload