I've been building a new ASP.NET MVC 5 application for quite a while now, and now that I'm on the final leg of the build of my application, something is very apparent to me. I have several post calls in my controllers and after each one, after validation is done, I follow PRG architecture which then allows me to pass some parameters to the URL for a GET request to take place that will then rebuild my ViewModel with the info I retrieved from the post as well as any info saved in the database, info hiding in construtors, etc. and I was curious, is this really the way things should be done?
My ViewModel is kind of complex, utilizing lists of Models to populate dropdownlists in my view, and I also have other models as variables in my viewmodel which I then populate to use appropriately in my application.
Of course, I have about 5 DropDownLists on one of my main views, and every time I do a post, I have to follow PRG and then call the repository layer to repopulate all of the dropdownlists again, even though they are the same info every time. I just figure there has to be a better way, but maybe I'm wrong.
Code samples:
public ViewResult RgaByRgaNumber(string strRgaNumber)
{
var vm = new ReturnGoodsAuthorizationViewModel()
{
RGA = _returnGoodsAuthorizationRepository.GetSpecificByRGANumber(strRgaNumber),
RGAItems = _returnGoodsAuthorizationItemRepository.GetByRgaNumber(strRgaNumber),
ReturnGoodsAuthorizations = _returnGoodsAuthorizationRepository.GetAll(),
ReasonCodes = _reasonCodeRepository.GetAll(),
RestockFeeOptions = _restockFeeOptionRepository.GetAll(),
Customers = _customerRepository.GetAll(),
IsUserAllowedToClickOpenCloseRGAButton = _returnGoodsAuthorizationRepository.GetUserAllowedToClickOpenCloseRGAButtonStatus(User.Identity.NameWithoutDomain())
};
vm.SubmitButtonText = vm.GetSubmitButtonText();
vm.RGAClosedOpenStatusText = vm.GetRgaClosedOpenStatusText();
vm.Customer = vm.Customers.First(x => x.CustomerId == vm.RGA.CustomerNumber);
vm.Items = _itemRepository.GetAll(vm.RGA.CustomerNumber);
return View("Index", vm);
}
public ViewResult Index()
{
var vm = new ReturnGoodsAuthorizationViewModel()
{
ReturnGoodsAuthorizations = _returnGoodsAuthorizationRepository.GetAll(),
ReasonCodes = _reasonCodeRepository.GetAll(),
RestockFeeOptions = _restockFeeOptionRepository.GetAll(),
Customers = _customerRepository.GetAll(),
RGA = new ReturnGoodsAuthorization()
{
PreparedByUser = User.Identity.NameWithoutDomain().ToUpper(),
AuthorizedByUser = User.Identity.NameWithoutDomain().ToUpper(),
CreateUser = User.Identity.NameWithoutDomain().ToUpper()
}
};
return View("Index", vm);
}
public ViewResult GetCustomerItemList(string strCustomerNumber)
{
var vm = new ReturnGoodsAuthorizationViewModel()
{
ReturnGoodsAuthorizations = _returnGoodsAuthorizationRepository.GetAll(),
ReasonCodes = _reasonCodeRepository.GetAll(),
RestockFeeOptions = _restockFeeOptionRepository.GetAll(),
Customers = _customerRepository.GetAll(),
RGA = new ReturnGoodsAuthorization()
{
PreparedByUser = User.Identity.NameWithoutDomain().ToUpper(),
AuthorizedByUser = User.Identity.NameWithoutDomain().ToUpper(),
CreateUser = User.Identity.NameWithoutDomain().ToUpper()
}
};
vm.RGA.CustomerNumber = strCustomerNumber;
vm.Customer = vm.Customers.First(x => x.CustomerId == strCustomerNumber);
vm.Items = _itemRepository.GetAll(strCustomerNumber);
Session["ReturnGoodsAuthorizationViewModel"] = vm;
return View("Index", vm);
}
public ViewResult GetItemPrice(string strCustomerNumber, string strItemNumber)
{
if (Session != null && Session["ReturnGoodsAuthorizationViewModel"] != null)
{
var vm = (ReturnGoodsAuthorizationViewModel)Session["ReturnGoodsAuthorizationViewModel"];
vm.ReturnGoodsAuthorizations = _returnGoodsAuthorizationRepository.GetAll();
vm.ReasonCodes = _reasonCodeRepository.GetAll();
vm.RestockFeeOptions = _restockFeeOptionRepository.GetAll();
vm.Customers = _customerRepository.GetAll();
vm.RGA.CustomerNumber = strCustomerNumber;
vm.Items = _itemRepository.GetAll(strCustomerNumber);
vm.Item = vm.Items.First(m => m.Number == strItemNumber);
vm.SubmitButtonText = vm.GetSubmitButtonText();
vm.RGAClosedOpenStatusText = vm.GetRgaClosedOpenStatusText();
vm.IsUserAllowedToClickOpenCloseRGAButton = _returnGoodsAuthorizationRepository.GetUserAllowedToClickOpenCloseRGAButtonStatus(User.Identity.NameWithoutDomain());
Session["ReturnGoodsAuthorizationViewModel"] = vm;
return View("Index", vm);
}
else
{
var vm = new ReturnGoodsAuthorizationViewModel()
{
ReturnGoodsAuthorizations = _returnGoodsAuthorizationRepository.GetAll(),
ReasonCodes = _reasonCodeRepository.GetAll(),
RestockFeeOptions = _restockFeeOptionRepository.GetAll(),
Customers = _customerRepository.GetAll(),
RGA = new ReturnGoodsAuthorization()
{
PreparedByUser = User.Identity.NameWithoutDomain().ToUpper(),
AuthorizedByUser = User.Identity.NameWithoutDomain().ToUpper(),
CreateUser = User.Identity.NameWithoutDomain().ToUpper()
}
};
vm.IsUserAllowedToClickOpenCloseRGAButton = _returnGoodsAuthorizationRepository.GetUserAllowedToClickOpenCloseRGAButtonStatus(User.Identity.NameWithoutDomain().ToUpper());
vm.RGA.CustomerNumber = strCustomerNumber;
vm.Item = _itemRepository.GetItem(strCustomerNumber, strItemNumber);
vm.Items = _itemRepository.GetAll(strCustomerNumber);
Session["ReturnGoodsAuthorizationViewModel"] = vm;
return View("Index", vm);
}
}