I'm new to MVC & coming from an asp.net webforms background, so I still find I have that webforms mindset that I'm finding difficult to break. One problem in particular is, what to do with code I need to populate my shared view?
In asp.net webforms, lets say I have a label on my masterpage. To populate that label, all I need to do is place some code into the masterpage codebehind and all pages referencing this masterpage will see it.
In MVC however, I'm not sure where to place shared code. Currently, I have the following code replicated on all actions calling views that reference my shared view, so I can populate data on the shared view:
public ActionResult MyFirstView()
{
Account account = _accountRepository.GetAccountByEmail(System.Web.HttpContext.Current.User.Identity.Name);
List<Campaign> campaigns = new List<Campaign>();
campaigns = _campaignRepository.GetCampaignsByAccountId(account.AccountId);
LayoutModel model = new LayoutModel
{
AccountId = account.AccountId,
Email = account.Email,
Name = account.Name,
LogoPath = account.LogoPath,
};
foreach (Campaign campaign in campaigns)
{
model.AddCampaigns(campaign);
}
return View(model);
}
What should I do with this shared code, so I don't need to keep replicating it?
Thanks