我有代碼folows:如何在Html.Raw中使用ViewBag?
@Html.Raw("<html lang='@ViewBag.locale' dir='@ViewBag.direction'>")
然而,這將直接解釋爲字符串@ ViewBag.locale,而不是它的價值。
什麼是這樣做的正確方法,以及爲什麼我們要使用@ Html.Raw,而不是實際的HTML標記
我有代碼folows:如何在Html.Raw中使用ViewBag?
@Html.Raw("<html lang='@ViewBag.locale' dir='@ViewBag.direction'>")
然而,這將直接解釋爲字符串@ ViewBag.locale,而不是它的價值。
什麼是這樣做的正確方法,以及爲什麼我們要使用@ Html.Raw,而不是實際的HTML標記
它改成這樣:
@Html.Raw("<html lang=\"" + ViewBag.locale + "\" dir=\"" + ViewBag.direction + "\">")
雖然覺得這個長相更好:
<html lang="@Html.Raw(ViewBag.locale)" dir="@Html.Raw(ViewBag.direction)">
我看不出爲什麼你需要在這裏使用Html.Raw。也許如果你展示的不僅僅是這一行代碼,它可能是有道理的,但我懷疑它。你會用簡單的
<html lang="@ViewBag.locale" dir="@ViewBag.direction">
更好否則,如果你弄清楚爲什麼你需要使用Html.Raw,那麼你可以隨時與饒宗頤的答案去。
這應該是正確的解決方案。 – h3xStream