Questions tagged [asp.net-mvc-viewmodel]

View model is a class, that represents data model used in specific view.

View model has many roles:

  • View models documents view by consisting only fields, that are represented in view.
  • View models may contain specific validation rules using data annotations or IDataErrorInfo.
  • View model defines how view should look (for LabelFor,EditorFor,DisplayFor helpers).
  • View models can combine values from different database entities.
  • You can specify easily display templates for view models and reuse them in many places using DisplayFor or EditorFor helpers.
497 questions
3
votes
1 answer

MVC create a view model from multiple domain models distantly related

I have been searching for a way to join 2 distantly related domain models into one view model with no luck. I am working on an existing application and have been asked to add a field to a result of fax log search. My controller is returning a…
3
votes
1 answer

Project setup for modular .Net MVC application

Consider an MVC4/EF5 project: Creating a web application which will have 3 modules. Based on the customer license, we will enable or disable 1-3 of the modules. Each of the modules will deal with a handful of common tables (Users, Company,…
3
votes
1 answer

How do I pass an ID from View to the ViewModel as a parameter for GET function?

I'm creating a project using MVC, knockoutJS, Web API, Bootstrap and so forth, the database in use is MSSQL Server 2012. It's all working very well, the controllers have properly created CRUD operations. The data from DB is show in a grid table in…
3
votes
3 answers

Asp.net-mvc pass ViewModel to partial view

This maybe really stupid but I just can't figure it out... So here is the problem. I have a model say A and a model B. class A { B b = new B(); } class B { ... } Now, I have a ViewModel for B (but not for A). So, now I have a view storngly…
Aditi
  • 1,188
  • 2
  • 16
  • 44
3
votes
1 answer

Is it good to define properties of "Model" type in ViewModel, ASP.net MVC

I've read couple of posts on using ViewModel in ASP.net MVC and realized it is different from "ViewModel" in M-V-VM pattern. ViewModel are used in order to avoid having Model being accessed directly from View but is it good approach to have…
Rohit
  • 6,365
  • 14
  • 59
  • 90
3
votes
3 answers

What to do when Model is exactly the same as ViewModel?

I want to know what is best practice. I have been told to always create ViewModels and never use core Model classes for passing data to Views. That makes sense. Lets me separate the things out. But what is Model is exactly the same as ViewModel.…
3
votes
3 answers

How to convert DTO to View Model and then back again?

I'm using MVC 4 with the repository pattern and unit testing also. I have a typical controller that has simple CRUD functionality. I've separated my View Models from my DTOs and I would like to know the best way to convert between the 2: Models: I…
3
votes
2 answers

MVC validation lost in Knockoutjs post

I'm using MVC4 and knockout. I have a form on a page that is strongly typed to a viewmodel. In that viewmodel I have some validation defined, for instance: [Required(ErrorMessage = "Title is required")] public string Title { get; set; } If I do a…
Dan dot net
  • 6,119
  • 5
  • 28
  • 25
3
votes
1 answer

ASP.NET MVC ViewModel Good Practices

First i want to share with you this very interesting article about ViewModel : http://rachelappel.com/use-viewmodels-to-manage-data-amp-organize-code-in-asp.net-mvc-applications I have one questions Is it a good practice that all ViewModel classes…
3
votes
1 answer

MVC Model Instantiation

I'm not grasping some conceptual basics with MVC models and I'm hoping for some helpful clarification. In my MVC 4 web app, I have a view model that builds an IEnumerable for a DropDownList, and if I do this in my view: @model…
theog
  • 1,064
  • 2
  • 20
  • 33
3
votes
1 answer

MVC view rendering does not respect data passed in a view model

I am attempting to create an MVC action method that allows users to progressively review a sequence of database records. Each time a user posts an update, it is submitted to the database and a new record is retrieved and rendered, for…
2
votes
3 answers

How do I sort a list of entities using ViewModel instead of ViewBag?

I am using ViewBag to help me sort a list of students found within a list of classes. I have read that ViewBag is something that should be avoided at all costs attempting to build a proper MVC project. When viewing the page that the below code…
Ecnalyr
  • 5,792
  • 5
  • 43
  • 89
2
votes
2 answers

Why isn't my view model's property being sent back to the server?

ASP.NET Core 7 MVC - view model's property is not being sent back to the server. I have two entities that have a many-to-many relationship between them: public class Trip { [Key] public Guid TripId { get; set; } public string Name { get;…
2
votes
2 answers

Resuse ViewModel in Asp.net MVC

I have seen lots of blog about MVVM vs MVC and blogs that says thay MVVM is like MVC extension in Windows. I have one design issue, I have made one win application that uses MVVM , now i have to create a same application in Web, so i decided to MVC…
Miraj Baldha
  • 297
  • 1
  • 2
  • 13
2
votes
2 answers

Show only if a ViewModel has value in MVC

I have this class and many properties in it public class Course { public int CourseId { get; set; } public string CourseName { get; set; } public Universities? UniversityName { get; set;} public string Summary { get;…