c# - ViewModel vs. InputModel in Razor View -


As I have found, to split the MVC model and input models into the Vovod model (models representing the scene) There is a good practice (representation of data is entered by the user).

ViewModel gets the property of the input input model. InputModel carries data that can be edited by the user.

  Public class EmployeeInputModel {public string name {get; Set;} public ID? Department ID {Received; Set;}} Public Class Employee Employee Model {Public ILIT & lt; Department & gt; Controlling methods look like this:  
  Public Class Employee Controller: Controller (Employer) {Make Public Actionersalt () {var viewModel = New Employee ContentModel (Departments Service. Gate All (), New Staff InputModel ()); See Return (see model); } Create a Public Function (Employee InputModelModel) {try {EmployeeService.Create (...); Return Redirect Action ("Index"); } Catch (Exceptional) {var viewModel = Creating New Employee Model (DepartmentsServiceGetLeal), createModel; View return (see model)}}}  

The view looks like this:

  @ Model Agency StaffView Model @ Html.EditorFor (m = & gt; m .EmployeeModel)  

The editor is partially the same:

  @Employer InputModel @html TextboxFor (M = & gt; m.Name) @html.propdowndown (m = & gt; m.Department, ???)  

This works great for me when Until I came to the point of dropdown list (see the departments in the sample). Because the editor templates do not know the view modal, but only the input modell.

I do not want to put the department list in the input model, because it is considered for this list (I have to bind them). It should be in the view modal. The properties of the input model should also not be visible in the modal.

Does anyone have any idea how to isolate the Viododal and Input model in one scene?

You must abstract a model of your departments to something like this:

  Public class EmployeeInputModel {public string name {get; Set;} public list & lt; Department InputModel & gt; Department {set; Set;}} public class department inputmodel {public entry id; The name of the public string; }  

Then you can display only the name of the department in the drop down list. After that the value will be the ID of the department.

You can take a look at an example.


Comments