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

EmailAttribute is not validating correctly in a ViewModel from ASP.NET Core

This is a field located in my viewmodel: [Required(ErrorMessage = "Email is missing."), EmailAddress(ErrorMessage = "Email is not valid.")] public string Email { get; set; } (EmailAddress is from the EmailAddressAttribute.EmailAddressAttribute()…
8
votes
5 answers

Inserting line breaks in DisplayName attributes

In my ViewModel there is a property that needs a 2 line label but when I place a
in the DisplayName attribute the HTML code is printed to the page instead of being interpreted as a line break. Is there a way to get a DisplayName to have a…
Matthew Verstraete
  • 6,335
  • 22
  • 67
  • 123
8
votes
2 answers

How to use ViewModels in ASP.NET MVC?

I just started learning about ViewModels in ASP.NET MVC. So, I thought of implementing a sample example as below: Business Entity public class AddModel { public int a { get; set; } public int b { get; set; } public int Add() { …
Sai Avinash
  • 4,683
  • 17
  • 58
  • 96
7
votes
3 answers

ViewModel Implementation in ASP.NET MVC - Is this code best practice?

I've just started to get into using ViewModels. Can you guys check out this code to see if I'm following best practice? Is there anything out of the ordinary? Would you do the validation differently? Sorry if code is lengthy (there's so many parts…
SaltProgrammer
  • 1,045
  • 2
  • 13
  • 29
7
votes
3 answers

Can we use only DTO instead of ViewModel?

We currently use DTO for Web API request and response and use ViewModel to pass data to the View in MVC Currently, we have: DTO as a separate project ViewModel is inside the UI project (another folder along with Controllers) Only difference I…
Sahil Sharma
  • 3,847
  • 6
  • 48
  • 98
7
votes
5 answers

Always use ViewModel pattern in MVC?

In MVC in the controller you should get the Model from DB and convert it to a ViewModel before sending it to the View. Typically using something like Automapper. My question is, if you need to show all the properties of the Model in the view as they…
Ricardo Polo Jaramillo
  • 12,110
  • 13
  • 58
  • 83
7
votes
4 answers

MVC View ViewModel HttpPost return value is always NULL

I'm passing a ViewModel back from my View to the Controller via a form HttpPost. However, the values returned are always NULL. ViewModel public class vmCompanyAddress { public StatelyTechAdmin.Models.Company Company { get; set; } public…
RobHurd
  • 2,041
  • 7
  • 26
  • 34
7
votes
4 answers

MVC 5 ViewModel not working as it was in MVC 4

I will try to get straight to the point. I had some help building a ViewModel here on StackOverflow. It worked fine in MVC 4 but now that I am converting the application to MVC 5 it is not working. Nothing has changed in way of the code. I have a…
7
votes
1 answer

Naming Convention for ViewModel Child Objects

I am building an ASP.Net MVC application using a ViewModel approach to keep my domain entities separate from the "models" used by my UI. I am using the following convention for naming my ViewModel classes. ViewModelName = ViewName + "ViewModel". For…
7
votes
2 answers

ASP.NET MVC 4 ViewModel With Child Interface

Is there a way to handle this without custom Model Binding? public class MyViewModel { public string UserId { get; set; } public IJob Job { get; set; } } public interface IJob { public long Id { get; set; } public string CompanyName { get;…
Adam Levitt
  • 10,316
  • 26
  • 84
  • 145
6
votes
1 answer

ASP.NET MVC Core date only format is not working using DataAnnotation

Following viewmodel used in a view is supposed to display a StartDate as, say 9/30/2015. But it is displaying as 9/30/2015 12:00:00 AM. How can I make it display without time while using DataAnnotaion? I know I can use…
nam
  • 21,967
  • 37
  • 158
  • 332
6
votes
3 answers

Data validation for every item in a list of my ViewModel

To make a validation with a Regex, I usually do: // In my ViewModel [RegularExpression("MyRegex", ErrorMessageResourceName = "MyErrorMessage")] public string MyField { get; set; } And the HTML helper @Html.TextBoxFor(model =>…
Pierre Arlaud
  • 4,040
  • 3
  • 28
  • 42
6
votes
2 answers

ASP.NET MVC with nested view model and Knockout

Cannot get my brain around how to implement knockout for the following ASP.NET MVC 4 nested view model : public class MyProfile { public string Name { get; set; } public IList List1 { get; set; } public IList List2 { get;…
6
votes
4 answers

Incorrect model property value rendered in partial view

I have a strongly-typed partial view whose model contains a property with the same name as the parent page's view model. For some reason the rendering engine is rendering the parent view model value, not the expected value (well, the value I expect…
Tim Croydon
  • 1,866
  • 1
  • 19
  • 30
5
votes
1 answer

Why is my ViewModel empty on [HttpPost]? .NET MVC 3

I'm trying my hardest to use ViewModels correctly in my web application, but I'm running into various problems. One of which, is if I set a breakpoint just after I post using a Create action, my viewModel hasn't stored any of my form values. I must…
Kiada
  • 679
  • 3
  • 15
  • 24
1
2
3
33 34