Why use view models mvc




















Tagged as ASP. Stats Please Sign up or sign in to vote. Copy Code. Founder 1TCompany AB. Github Blog Twitter StackOverflow. Go to top. Layout: fixed fluid.

Here the word Employee represents the Controller name, the word Details represent the action method name. As it is a view model so we prefixed the word ViewModel. Although it is not mandatory to follow this naming convention, I personally feel it is good to follow this naming convention. As you can see in the above code, here we are passing the employee details view model as a parameter to the view.

And one more thing you need to notice is that now we are not using any ViewData or ViewBag within our Details action method. First, add a folder with the name Employee within the Views folder of your application. Once you add the Employee Folder, then you need to add a view file with the name Details. I would like to have your feedback. Invest In Learning. Submit Query Please Wait Training Schedules. Have Queries? Live Training Schedules.

We use cookies to make interactions with our websites and services easy and meaningful. Please read our Privacy Policy for more details. Remember me Forgot Password? Unlike before, we are going to check the "Create a strongly-typed" view checkbox. We are then going to select our "Album" class within the "View data-class" drop-downlist.

This will cause the "Add View" dialog to create a View template that expects that an Album object will be passed to it to use. Notice the first line, which indicates that this view is strongly-typed to our Album class.

The Razor view engine understands that it has been passed an Album object, so we can easily access model properties and even have the benefit of IntelliSense in the Visual Web Developer editor. Notice that IntelliSense is triggered when you enter the period after the Model keyword, showing the properties and methods that the Album class supports. We'll see details of an Album like below.

Now we'll make a similar update for the Store Browse action method. Update the method so it returns an ActionResult, and modify the method logic so it creates a new Genre object and returns it to the View. Right-click in the Browse method and select "Add View…" from the context menu, then add a View that is strongly-typed add a strongly typed to the Genre class.

We'll see the Browse page displayed like below. Finally, let's make a slightly more complex update to the Store Index action method and view to display a list of all the Genres in our store. We'll do that by using a List of Genres as our model object, rather than just a single Genre. First we'll change the model declaration to indicate that the view will be expecting several Genre objects rather than just one.

This tells the Razor view engine that it will be working with a model object that can hold several Genre objects. Next, we'll loop through the Genre objects in the model as shown in the completed view code below. Notice that we have full IntelliSense support as we enter this code, so that when we type " Model. Next, the scaffolding feature examined the Genre object and determined that each will have a Name property, so it loops through and writes them out.

It also generates Edit, Details, and Delete links to each individual item. We'll take advantage of that later in our store manager, but for now we'd like to have a simple list instead. That works, but it could lead to trouble later since it relies on a hardcoded string. For instance, if we wanted to rename the Controller, we'd need to search through our code looking for links that need to be updated. The Html.



0コメント

  • 1000 / 1000