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
240
votes
11 answers

ViewModel Best Practices

From this question, it looks like it makes sense to have a controller create a ViewModel that more accurately reflects the model that the view is trying to display, but I'm curious about some of the conventions. Basically, I had the following…
jerhinesmith
  • 15,214
  • 17
  • 62
  • 89
67
votes
6 answers

Should a service layer return view models for an MVC application?

Say you have an ASP.NET MVC project and are using a service layer, such as in this contact manager tutorial on the asp.net site: http://www.asp.net/mvc/tutorials/iteration-4-make-the-application-loosely-coupled-cs If you have viewmodels for your…
erg39
  • 817
  • 1
  • 6
  • 13
43
votes
3 answers

How to pass model to partial view

I have two view models: public class ParentViewModel { public Id { get; set; } ..... public ChildViewModel Child{ get; set; } } public class ChildViewModel { public ChildId { get; set; } ..... …
DarkNik
  • 803
  • 2
  • 8
  • 17
32
votes
3 answers

The model item is of type CookMeIndexViewModel, but requires a model item of type IEnumerable

I am following along with the music store example to try learn ASP.NET MVC. I'm creating a cookbook application. I have created my viewmodel that looks like this: namespace CookMe_MVC.ViewModels { public class CookMeIndexViewModel { …
Diver Dan
  • 9,953
  • 22
  • 95
  • 166
30
votes
5 answers

Using a PagedList with a ViewModel ASP.Net MVC

I'm trying to using a PagedList in my ASP.Net application and I found this example on the Microsoft website…
ltedone
  • 639
  • 1
  • 6
  • 12
24
votes
2 answers

ViewModels in MVC / MVVM / Separation of layers- best practices?

I'm fairly new to the using ViewModels and I wonder, is it acceptable for a ViewModel to contain instances of domain models as properties, or should the properties of those domain models be properties of the ViewModel itself? For example, if I have…
Iain
  • 1,724
  • 6
  • 23
  • 39
21
votes
1 answer

MVC ViewModel example

I've been doing tutorials and trying to learn best practice when it comes to MVC development. The design I'm using below comes from Pro ASP.Net MVC5 by Apress/Adam Freeman. So far, everything is coming along good...but I still have not completely…
JoshYates1980
  • 3,476
  • 2
  • 36
  • 57
20
votes
5 answers

How can I refactor my database access code outside my MVC project but keep my viewmodels inside?

I have an asp.net-mvc website with the following folders: Controllers Scripts Views ViewModels Models DomainModel I now want to access a lot of this business logic and database access code and data in another .net app (a windows console app so not…
leora
  • 188,729
  • 360
  • 878
  • 1,366
19
votes
5 answers

Client form validation not working with modal dialog in MVC

I am changing a create form to become a modal dialog and jquery Unobtrusive validation stops working and don't know how to fix it. Index.cshtml has a link to trigger a modal dialog. Create @section…
19
votes
5 answers

ViewBag vs Model, in MVC.NET

This is more of a generic architectural question: I'm trying to decide if it's ok for my programmers to use "ViewBags" to pass data to views that already accept Models. My personal preference is to avoid ViewBags and build Robust Models containing…
DotNet98
  • 727
  • 3
  • 12
  • 24
17
votes
3 answers

ASP.NET MVC View Model not binding on HTTP Post with DropDownList

I am having an issue that when I post to a controller I lose binding and everything in my view model is NULL. Here is the code I am using: View: @model ArticleCategoryVm @using (@Html.BeginForm()) { @Html.HiddenFor(model =>…
Danny Brady
  • 1,895
  • 4
  • 20
  • 30
10
votes
2 answers

ViewData and ViewModel in MVC ASP.NET

I'm new to .Net development, and now are following NerdDinner tutorial. Just wondering if any of you would be able to tell me What is the differences between ViewData and ViewModel (all I know is they are used to pass some form of data from…
Amy Lee
  • 277
  • 3
  • 15
10
votes
3 answers

Return two lists to the view c# mvc

I have two lists that i want to return to the view, 'added' and 'removed'. However currently i can only return either the 'added' or 'removed'. How can i return both in one object? I am very new to MVC so apologies if this code is messy! public…
Marc Howard
  • 395
  • 2
  • 6
  • 25
9
votes
7 answers

MVC - Passing multiple data tables to a view

I currently have the following code in the HomeController of my MVC project: public class HomeController : Controller { public ActionResult Index() { MyDataContext dc = new MyDataContext(); IQueryable j = …
Paul Michaels
  • 16,185
  • 43
  • 146
  • 269
9
votes
2 answers

How can I make the Required attribute for my custom DropDownList editor template operate client side?

I have an editor template for DropDownLists that is marked with an attribute like this: [AttributeUsage(AttributeTargets.Property)] public class DropDownListAttribute : UIHintAttribute { public string SelectListName { get; set; } public…
ProfK
  • 49,207
  • 121
  • 399
  • 775
1
2 3
33 34