mercredi 14 mai 2014

How to use Automapper to map controller methods to viewmodel properties for a asp.net mvc application?


Vote count:

0




I am trying to leverage the Automapper for mapping the controller methods to the viewmodel properties in an asp.net mvc project. I analyzed few articles about Automapper and found that it is wonderful object to object mapper between complex model object and viewmodel object. I have code in a controller: CustomersController.cs



[Authorize(Roles = "Administrator")]
public ActionResult Index()
{
var user = _userService.GetUser(_profile.UserName);
if (!user.IsActive)
return RedirectToAction("");

var clientGroups = new List<ClientGroup>();

var model = new CustomerGroupsIndexViewModel()
{

CustomerGroupUsersUrl = Url.RouteUrl<CustomerGroupsController>(c => c.GetUsersForCustomerGroup(null, null, null, 0, 0)),
CustomerGroupByAreaUrl = Url.RouteUrl<CustomerGroupsController >(c => c.GetAreaDetailsForCustomerGroup(null, null, null, 0, 0)),

CheckLoginNameUrl = Url.RouteUrl<UsersController>(c => c.CheckLoginName(null)),
ResetUserUrl = Url.RouteUrl<UsersController>(c => c.ResetPassword(null)),
GetSelectOptionsForCustomerGroupUrl = Url.RouteUrl<ClientGroupsController>(c => c.GetSelectOptionsForCustomerGroup(null,null)),

FindUsersMatchingTermUrl = Url.RouteUrl<UsersController>(c => c.FindUsersMatchingWithLoginName(null)),
NumberOfTestTaken = _scanService.GetCustomerForUser(user).Count(),
RefreshCustomerGroupUrl = Url.RouteUrl<CustomerGroupsController >(c => c.RefreshClientGroup()),
};

Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();

return View("CustomerGroupIndex", model);
}


I have such methods across the project code base. Can anyone help me how can I use Automapper efficiently here?


Thanks & Regards, Santosh Kumar Patro



asked 1 min ago






Aucun commentaire:

Enregistrer un commentaire