簡體   English   中英

MVC4:部分視圖中的訪問控制器屬性

[英]MVC4: Access controller properties in partial view

是否可以從局部視圖訪問基本控制器的屬性?

我有以下設置:

public class BaseController : Controller
{
    private string ServerName
    {
        get
        {
            return Request.ServerVariables["SERVER_NAME"];
        }
    }
    private Entities.Client _Client { get; set; }
    public Entities.Client Client
    {
        get
        {
            return this._Client ?? (this._Client = this.HttpContext.Application["Client"] as Entities.Client);
        }
    }
    private Settings _Settings { get; set; }
    public Settings Settings
    {
        get
        {

            if (this._Settings == null)
            {
                this._Settings = new Settings(this.Client, this.Client.WebPageTemplateCapabilities != null ? SettingsType.XML : SettingsType.SQL);
            }

            return this._Settings;
        }
    }
}

我的所有控制器都繼承了BaseController,在這些控制器的子操作的一些視圖中,我渲染了部分視圖。 有沒有辦法從其中一個部分視圖訪問BaseController.Settings?

視圖所需的任何信息應該控制器傳遞到視圖,然后進一步從視圖傳遞到部分,例如

public ActionResult Index()
{
    return View(this.Settings);
}

在你看來

@model Settings

@Html.RenderPartial("SomePartial", Model)

在你的部分

@model Settings

// use settings

我的所有控制器都繼承了BaseController,在這些控制器的子操作的一些視圖中,我渲染了部分視圖

在這種情況下,您只需要從控制器傳遞模型,例如

public ActionResult SomeAction()
{
    return PartialView("SomePartialView", this.Settings);
}

我最終這樣做了:

@{
    var settings = (ViewContext.Controller as BaseController).Settings;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM