asp.net-mvc-viewmodel

How to use ViewModels in ASP.NET MVC?

半城伤御伤魂 提交于 2019-11-28 09:20:56
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() { return (this.a + this.b); } } Add ViewModel public class AddViewModel { public AddModel addModel; public int Total { get; set; } } Controller public class AddController : Controller { [HttpPost] public JsonResult Add(AddViewModel model) { int iSum = model.addModel.a + model.addModel.b; model.Total = iSum; return Json(model); } public ActionResult Index() { return View();

MVC ViewModel example

杀马特。学长 韩版系。学妹 提交于 2019-11-27 19:26:41
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 come to grip on working with Controllers. Yes, I understand the concept of Controllers, but still struggle when it comes to post and get methods. Here is the flow of my sample MVC application: My app.Domain project I have a user table in the database and reference it with Entities/Users.cs using System; using System.Collections.Generic; using System

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

纵饮孤独 提交于 2019-11-27 19:00:51
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 { public int NumberOfReceipes { get; set; } public List<string> ReceipeName { get; set; } } } my controller looks like this public ActionResult Index() { var meals= new List<string> { "Dinner 1", "Dinner 2", "3rd not sure" }; //create the view model var viewModel = new CookMeIndexViewModel { NumberOfReceipes = meals.Count(), ReceipeName = meals }; return View

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

ε祈祈猫儿з 提交于 2019-11-27 17:34:54
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 views, is the service layer the appropriate place to provide each viewmodel? For instance, in the service layer code sample there is a method public IEnumerable<Contact> ListContacts() { return _repository.ListContacts(); } If instead you wanted a IEnumerable, should it go in the service layer, or is there somewhere else that is the "correct" place?

How to pass model to partial view

人走茶凉 提交于 2019-11-27 17:07:57
问题 I have two view models: public class ParentViewModel { public Id { get; set; } ..... public ChildViewModel Child{ get; set; } } public class ChildViewModel { public ChildId { get; set; } ..... } Controllers: public ActionResult Index() { .... <some code> return View("NewIndex", ParentViewModel); } [HttpPost] public ActionResult PartialAction(ChildViewModel childView) { return RedirectToAction("Index"); } And views: Index @model ParentViewModel .... @Html.Partial("_Partial", Model.Child) and

Using a PagedList with a ViewModel ASP.Net MVC

不羁岁月 提交于 2019-11-27 03:31:46
I'm trying to using a PagedList in my ASP.Net application and I found this example on the Microsoft website http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application How is it possible to use a PagedList in a complex situation that uses a ViewModel? I'm trying to add a PagedList without success to the Instructor example posted here: http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/reading-related-data-with-the-entity-framework-in-an-asp-net-mvc-application The problem is that the

MVC - Passing multiple data tables to a view

不羁岁月 提交于 2019-11-27 02:54:10
问题 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<Table1Data> j = from n in dc.Table1 select n; return View(j); } So that works okay, but now I want to pass a second table through to the same view. So I was thinking I should be able to do something like this: public class HomeController : Controller { public ActionResult Index() { MyDataContext

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

耗尽温柔 提交于 2019-11-26 22:45:13
问题 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 { public int NumberOfReceipes { get; set; } public List<string> ReceipeName { get; set; } } } my controller looks like this public ActionResult Index() { var meals= new List<string> { "Dinner 1", "Dinner 2", "3rd not sure" }; //create the view model var viewModel =

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

有些话、适合烂在心里 提交于 2019-11-26 19:05:27
问题 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 views, is the service layer the appropriate place to provide each viewmodel? For instance, in the service layer code sample there is a method public IEnumerable<Contact> ListContacts() { return _repository.ListContacts(); } If instead you wanted a

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

浪子不回头ぞ 提交于 2019-11-26 16:37:36
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 a class Album.cs public class Album { public int AlbumId { get; set; } public string Title { get; set; } public string Price { get; set; } public virtual Genre Genre { get; set; } public virtual Artist Artist { get; set; } } Would you typically have the ViewModel hold an instance of the Album.cs class, or would you have the ViewModel have properties