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
2
votes
1 answer

how to bind json data to existing viewmodel - mvc5

I have a long strongly-typed form which its inputs are bound with viewmodel as html helpers, while i have a table that's not strongly-typed, it's generated when user clicks Add button, and i collect its data as json. how to map json data to…
2
votes
0 answers

AutoMapper Infinite Loop using EF Code First

I have the following classes (One-One relationship Asset-TrackingDevice): public class Asset { public int Id { get; set; } public string Name { get; set; } public TrackingDevice TrackingDevice { get; set; } } public…
2
votes
1 answer

Partial View with variable model type

Update from Travis Answer : public interface IEntity { int Id{get;set;} string Name{get;set;} } public class Vehicule:IEntity { public int Id{get;set;} public string Name{ get; set; } } public class Sector:IEntity { public string…
2
votes
2 answers

How to configure a ViewModel and Partial Page to include an IEnumerable

I'm trying to change my ASP.NET MVC project from straight Views and Models to using a ViewModel intermediate layer. Basically, what I'm doing includes having a Person with zero or more Events associated to them. I'm changing to the ViewModel…
techturtle
  • 2,519
  • 5
  • 28
  • 54
2
votes
1 answer

Should I store view models or domain models in session?

Am a little confused about session state storage. I have an MVC application, there are view models, which are closer to the view, then there are the domain models which have rich behavior. We are required to store some session state in our…
2
votes
1 answer

c# mvc: Persisting data from View to Controller and then via single object for List

I am working with a View that returns List to the Controller's Create action method. Questions: Is it correct to return List to the controller? The reason I am doing is that I have to make multiple rows entry to the DB for…
PineCone
  • 2,193
  • 12
  • 37
  • 78
2
votes
2 answers

Separate ViewModel for Read, Create, and Update actions in ASP.NET MVC

I use the same ViewModel in ASP.NET MVC projects, but for a thousand of records it seems to be better not to retrieve unused records from database. For example, assume UserViewModel for Read, Create and Update situations as shown below: public class…
Jack
  • 1
  • 21
  • 118
  • 236
2
votes
1 answer

MVC ViewModel returns ArgumentNullException

I am having a problem returning values to the controller when using a ViewModel. For clarity I have simplified the code below where the original has many more fields. When the page is loaded, the value in the hidden field is as expected. However…
2
votes
1 answer

Passing multiple models to _Layout.cshtml and one of them creates a dynamic menu

Please be gentle on me. I've read plenty of MVC answers for others on this site but now I've got something where I cannot find an answer. Going through lessons I created a LoginDetails project where a viewmodel was passed from the controller to…
Tilting Code
  • 137
  • 1
  • 3
  • 13
2
votes
1 answer

MVC ViewModel Binding Construction vs. Flattening

In my ViewModel (also in my Domain model), I have kinda dynamic Property Structure where the Profile Elements are a List of the base class ProfileVM and refer to a ProfileDefinitionElement (just to explain the ViewModel without pasting the full…
Yves
  • 51
  • 7
2
votes
0 answers

How to do calculations in an EDIT viewmodel with javascript? and html.editorfor inputs

I have been struggling for days trying to figure out this problem. Now, I am not too familiar with JavaScript but I have tried many ideas and solutions given on the internet and still cannot get JavaScript to work for me Here is what I am trying to…
2
votes
0 answers

MVC 6 - ViewModel Builder Concept

I started refactoring an ASP.Net 5 web application which uses MVC 6 and Entity Framework 7 when I was wondering about some points. My controllers currently use the DbContext implementation via dependency injection to fill the view models and return…
2
votes
1 answer

What is the best practice of passing data from a controller to a view layout?

I currently have a MVC site that needs to have dynamic content on the header of every page. I currently get the required data as normal in the controller and place it in a View Model. In the view, I take the data and stick the template parts in to…
Wil
  • 10,234
  • 12
  • 54
  • 81
2
votes
2 answers

Bind DropDownListFor with List on ViewModel

I'm trying to do this: This is my ViewModel and Model: public class OpeningYearViewModel { public int OpeningYearId { get; set; } public string Description { get; set; } public List GradesList { get; set; } } public class…
2
votes
4 answers

.NET MVC -- Use a class as the model?

In my MVC app, several view models are going to pretty much identical. Rather than replicate the model each time, I'm thinking I could just create a class instead. What I'm not sure about then is how to include that class in each model. For…
Casey Crookston
  • 13,016
  • 24
  • 107
  • 193