# Returning a viewmodel

In my ASP.NET MVC code, I like to use controller service classes for my controllers. These service classes contain methods for retrieving viewmodel objects.

Here is an example controller snippet:

public SubscriptionsController(ISubscriptionsControllerService service)
{
_service = service;
}

public ActionResult Index(Guid id)
{
return View("Subscriptions", _service.GetSubscriptionsViewModelOnGet(id));
}

[HttpPost]
public ActionResult Index(SubscriptionsViewModel viewModel)
{
_service.SaveSubscriptions(viewModel);

return View("Subscriptions", _service.GetSubscriptionsViewModelOnPost(viewModel));
}


As you can see, I have a method for retrieving the subscriptions viewmodel on a GET request, as well as the equivalent for a POST request.

The POST method takes in an existing viewmodel object and updates any relevant data e.g. a list of subscription items, that need to be refreshed before passing back to the view.

My question is whether the naming of the methods (GetSubscriptionsViewModelOnGet() and GetSubscriptionsViewModelOnPost()) makes sense. They seem OK to me, but I'm interested in other people's views.

Why not name them both the same? The difference is the type of parameter you're passing. This leaves you free to do some method overloading:

public SubscriptionsViewModel GetSubscriptionsViewModel(Guid id)
{
//GET Logic here...
}

public SubscriptionsViewModel GetSubscriptionsViewModel(SubscriptionsViewModel viewModel)
{
//POST Logic here...
}


Why call them the same? They both do the same: return a SubscriptionsViewModel.

• Yeah, I was thinking the same thing this morning, just before I posted this question. I think it does not matter about the context of when the code is getting the viewmodel e.g. GET/POST. I can make the method names easier i.e. the same. – Jason Evans Jul 10 '13 at 15:10