2

2 つの変数 (mvcHtmlString 型) を比較したいのですが、応答は常に false です...

@{ //Load the good css file

    if (ViewBag.BrowserName == MvcHtmlString.Create("ie"))
    {
        if (ViewBag.BrowserVersion > 9)
        {
            @Styles.Render("~/Content/ie10-css")
        }
        else
        {
            @Styles.Render("~/Content/ie7-css")
        }
    }
    else if (ViewBag.BrowserName == MvcHtmlString.Create("safari")) //and ipad
    {
        @Styles.Render("~/Content/safari/css")
    }
    else  //if (ViewBag.BrowserName == "firefox" || ViewBag.BrowserName == "chrome")
    {
        @Styles.Render("~/Content/default/css")
    }

}

私のコンソールショー:

MvcHtmlString.Create("safari")                 -> {safari}
ViewBag.BrowserName                            -> {safari}
ViewBag.BrowserName == MvcHtmlString("safari") -> false

なぜそれが偽なのかと尋ねます。

4

2 に答える 2

0
public String GetBrowserName()
{
    ViewBag.logged = false;
    return (Request.Browser.Browser.ToLowerInvariant());
}

したがって、Html.Action は MvcHtmlString を返します (理由はわかりません) が、強制的に cshtml ページで直接文字列にすることができます。

@if (ViewBag.BrowserName == null)
{
    ViewBag.BrowserName = Html.Action("GetBrowserName", "Services").ToString();
}

2 つの文字列を比較します。

if (ViewBag.BrowserName == "safari")
于 2013-09-11T09:48:09.360 に答える