简体   繁体   中英

ASP.NET MVC: Where to place code populating shared view?

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM