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
4
votes
2 answers

Having to repopulate viewmodel when modelstate is invalid due to not sending all data in such as drop down box list

In my project I create a survey, I link this survey to a company and select users to participate. I select survey template, add extra questions and sets information about the survey such as start and end date. All in all the result is a complex view…
4
votes
2 answers

MVC ASP.NET No parameterless constructor defined for this object

When I click submit in order to create an account the following error is displayed. I can't find why it is giving the following error. Also I tried to use breakpoints and debugging but it was useless No parameterless constructor defined for this…
4
votes
1 answer

Interface for ViewModel Builders in MVC

I've been trying to make up my mind on how to deal with ViewModels in my MVC solution. I think, after reading different posts and blogs, that the best approach is having builders that implement an interface and then inject those into the controller…
mitomed
  • 2,006
  • 2
  • 29
  • 58
4
votes
3 answers

Decoration on ViewModel property to use a different name for binding

On MVC3, is there a way to decorate a ViewModel property in order to get the DefaultModelBinder to use a different name for it in the request? For example, suppose you have the following view model: public class SomeModel { public string…
Pablo Romeo
  • 11,298
  • 2
  • 30
  • 58
4
votes
2 answers

Show multiple models in a single view using ViewModel in Razor MVC3 (with only Details in view)

My task is to show multiple models into a single view.I've created a ViewModel for my requirement but I'm not meeting my requirement. please have a look into the below code and rectify me where m i going wrong ??? public partial class…
Rabbil
  • 43
  • 1
  • 8
3
votes
2 answers

ASP.NET MVC best practices using EntityFramework and mapped ViewModels

I've never worked with MVC design pattern before and recently I started to work on the project using ASP.NET MVC. I'm using ActiveRecord as my data layer. I'm also using view models (unique for each view) and AutoMapper to map my view models to…
Burjua
  • 12,506
  • 27
  • 80
  • 111
3
votes
1 answer

Dropdown List from View Model Result

I have a working example of a 'ViewModel' that I am thinking will work for a dropdown list. Currently I am using this to do a Foreach and it works fine for that but I need the same result in a dropdown on my form. I have played around with different…
S.Purtan
  • 125
  • 1
  • 2
  • 11
3
votes
1 answer

custom model binder with complex type MVC 3

I have the following ViewModel and i would like to create a custom binder to bind subclasses (LogOnModel, ChangePasswordModel). public class LogOnViewModel { public string NextStep { get; set; } public string PreviousStep { get; set; } …
3
votes
2 answers

ASP.NET MVC ViewModel validation and Single Responsibility Principle

The most common approach I've seen in online examples when it comes to validation in MVC is to use the ViewModel to validate the data. People either use Data Annotations or implement IValidatableObject and IClientValidatable interfaces. But doesn't…
Mateusz Dudek
  • 103
  • 1
  • 6
3
votes
3 answers

Asp.Net MVC - Model Binding to Model or ViewModel

I have a controller, which returns a view passing in a view model, which has properties required for display of the view (Drop-down select item lists etc). But when I post it to the server, I have a different model class, which has the selected…
Adi Sekar
  • 43
  • 1
  • 1
  • 8
3
votes
2 answers

What is the definition of "ViewModel Behavior"?

I have been reading about the difference between DTO's and Viewmodels, in which the main one is the presence of behaviors in the Viewmodels compared to DTOs which just carry data through layers, but is not clearly stated the definition for behaviors…
CoderRoller
  • 1,239
  • 3
  • 22
  • 39
3
votes
2 answers

Iterating ICollection from ViewModel within View

I have two autogenerated database models (Product and ProductDetails) which I merged into a ViewModel so I can edit all data at once. What confuses me is the part where I am supposed to iterate through ICollection of…
3
votes
2 answers

MVC 5 ViewBag security

I am coding an MVC internet application, and I have a question in regards to using the ViewBag. In many of my controllers, I have SelectList objects, where the user can select an object. The object that is selected is a foreign key value for my…
Simon
  • 7,991
  • 21
  • 83
  • 163
3
votes
1 answer

ASP.NET MVC 1 ViewModel per View?

I've read article "How we do MVC". In rule #3, said that "we only have one ViewModel type per View". For example, when we scaffolding in mvc, it's automatically create 5 Views (Create, Delete, Details, Edit, and Index). By that statement, is it mean…
Willy
  • 1,689
  • 7
  • 36
  • 79
3
votes
2 answers

Can I determine the model of a strongly typed ASP.NET MVC view before it is executed?

I would like to determine the view model type from a (strongly typed) ASP.NET MVC 4 View before the view is executed. My Controller logic enables me to determine the view name, and thus load the view programatically, however there doesn't appear to…
Will
  • 965
  • 5
  • 19