为部分视图标题中的每个页面指定不同的主体类

时间:2017-08-05 13:17:56

标签: c# asp.net-mvc

我刚刚接触到MVC,我想为每个视图设置不同的body类。 我的标题是部分视图,而@RenderSection不适用于它。

_Layout.cshtml:

    @{ 
        Html.RenderAction("GetHeader", "Main");
    }

    @RenderBody()

    @{
        Html.RenderAction("GetFooter", "Main");
    }

_HeaderLayout.cshtml:

    //...
    <body class=" here must be specified class different for each view">
    //...

MainController:

    public class MainController : Controller
    {
        public ActionResult GetHeader()
        {
            return PartialView("~/Views/Shared/_HeaderLayout.cshtml");
        }

        public ActionResult GetFooter()
        {
            return PartialView("~/Views/Shared/_FooterLayout.cshtml");
        }
    }

请问好吗?

4 个答案:

答案 0 :(得分:1)

我会以两种方式做到这一点:

  1. 为应用中使用的所有视图模型创建一个基本ViewModel类,并为BodyClass添加一个属性,然后在Partial View中实现它。
  2. 在返回部分视图之前,在ViewBag字典中添加属性。
  3. 实施例: 1.基础课

    public class BaseViewModel
    {
       public string BodyClass {get; set;}
    }
    

    用法:

    基类:

    部分视图中的

    @model BaseViewModel
    ///...
    <body class="@Model.BodyClass">
    
    控制器中的

     public ActionResult GetHeader()
     {
         var vm = new BaseViewModel { BodyClass= "test-class" };
         return PartialView("~/Views/Shared/_HeaderLayout.cshtml", vm);
     }
    

    ViewBag

    public ActionResult GetHeader()
    {
         ViewBag[SomeConstantStringValue] = "test-class";
         return PartialView("~/Views/Shared/_HeaderLayout.cshtml");
    }
    
    部分视图中的

    <body class="@ViewBag[SomeConstantStringValue]">
    

    请记住,您始终必须指定ViewBag值,否则您将收到错误。

答案 1 :(得分:0)

Mihail Stancescu的回答可能是最好的,但如果你不想要每种控制器方法的视图模型,你可以使用辅助函数代替。无论哪种方式,你可能不得不制作自己的方法来决定返回哪个类(参见下面的BodyClassForTabAndMethod())。

创建一个Helper类(如果你还没有合适的类):

public static class Helper
{
    public static string BodyClassForTabAndMethod()
    {
        string[] selectedTabAndMethod = GetSelectedTabAndMethod();

        string bodyClass = "";

        // Change the below switch statements based upon the controller/method name.
        switch (selectedTabAndMethod[0])
        {
            case "home":
                switch (selectedTabAndMethod[1])
                {
                    case "index":
                        return "IndexClass";
                    case "about":
                        return "AboutClass";
                    case "contact":
                        return "ContactClass";
                }
                break;
            case "account":
                switch (selectedTabAndMethod[1])
                {
                    case "login":
                        return "LoginClass";
                    case "verifycode":
                        return "VerifyCodeClass";
                }
                break;
        }

        return bodyClass;
    }

    public static string[] GetSelectedTabAndMethod()
    {
        string[] selectedTabAndMethod = new string[2]; // Create array and set default values.
        selectedTabAndMethod[0] = "home";
        selectedTabAndMethod[1] = "index";

        if (HttpContext.Current.Request.Url.LocalPath.Length > 1)
        {
            // Get the selected tab and method (without the query string).
            string tabAndMethod = HttpContext.Current.Request.Url.LocalPath.ToLower();

            // Remove the leading/trailing "/" if found.
            tabAndMethod = ((tabAndMethod.Substring(0, 1) == "/") ? tabAndMethod.Substring(1) : tabAndMethod);
            tabAndMethod = ((Right(tabAndMethod, 1) == "/") ? tabAndMethod.Substring(0, tabAndMethod.Length - 1) : tabAndMethod);

            // Convert into an array.
            if (tabAndMethod.Count(s => s == '/') == 1)
            {
                string[] split = tabAndMethod.Split('/');
                selectedTabAndMethod[0] = split[0];
                selectedTabAndMethod[1] = split[1];
            }
        }

        return selectedTabAndMethod;
    }

    public static string Right(string value, int length)
    {
        if (string.IsNullOrEmpty(value)) return string.Empty;

        return ((value.Length <= length) ? value : value.Substring(value.Length - length));
    }
}

然后在你看来: <body class="@Helper.BodyClassForTabAndMethod()">

答案 2 :(得分:0)

我为自己的问题找到了一个解决方案,非常简单:

在Global.asax://或任何其他在开始时加载的类

Sub Highlight_Intersections()
'tested 
    Dim c as Range
    For Each c in Range("B2:J10") 'you supply address
        If Cells(c.Row,1).Value = Cells(2,c.Column).Value Then
            c.Interior.ColorIndex = 6   '(Yellow)
        End If
    Next c
End Sub

在任何Controller中以及返回视图/局部视图之前:

    public static class WrrcGlobalVariables
    {
        //any other global variables...

        public static string BodyClass { get; set; }
    }

和_HeaderLayout.cshtml:

    public ActionResult Index()
    {
        //some codes...

        WrrcGlobalVariables.BodyClass = "HomePage";

        return View();
    }

答案 3 :(得分:0)

Mihail Stancescu给出的两种方法都是正确的,但是只有在需要时才有另一种方法可以获得默认值和自定义值。如果您所做的只是渲染部分而没有任何需要子控制器的额外逻辑,您也应该使用RenderPartial而不是RenderAction。

在_Layout.cshtml中

@Html.Partial("_HeaderLayout")
@RenderBody()
@Html.Partial("_FooterLayout")

在_HeaderLayout.cshtml

<body class="@ViewBag[SomeConstantStringValue]">

在_ViewStart.cshtml

@{
    ViewBag[SomeConstantStringValue] = ViewBag[SomeConstantStringValue] ?? "default-class";
}

然后在任何视图或控制器中的任何位置设置此ViewBag值,这样您就可以确保保证默认值以防止空引用异常