1

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

2
  • If it wasn't a MVC controller but a simple, normal class? The same... Commented Aug 26, 2016 at 15:38
  • Wouldnt it be better to store that stuff in the cookie using claims and just access the claims in the view/layout? Commented Aug 26, 2016 at 15:40

2 Answers 2

3

You can place shared code inside a method.

If that code is shared across multiple controllers, then you can make a base-class for your controllers, and derive from your base class instead of from Controller

If you're doing the same thing in all of your actions You can override Controller.Initialize, and write code there.

Sign up to request clarification or add additional context in comments.

Comments

1

You could use a Child Action. The idea is to put this shared code in a special controller action:

public ActionResult MySharedAction()
{
    Account account = _accountRepository.GetAccountByEmail(System.Web.HttpContext.Current.User.Identity.Name);
    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 PartialView(model);
}

then have a corresponding partial view to display this shared view model:

@model LayoutModel

...

which you could include in your _Layout.cshtml at the desired location:

<div>@Html.Action("MySharedAction", "SomeController")</div>

The child action even if it runs in the same HTTP pipeline as the main action it is isolated from it and your main action doesn't need to know about this common shared logic.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.