Razor doesn't understand unclosed html tags

前端 未结 4 923
灰色年华
灰色年华 2020-12-04 17:11

With RazorViewEngine, I can do this:

if (somecondition) {
     
some stuff
}

but I can\'t seem to do this (Razor ge

4条回答
  •  春和景丽
    2020-12-04 17:43

    You can create a custom MVC Helper method. For with you create a public static class MyRenderHelpers in namespace System.Web.Mvc.Html and write a method Html.

    namespace System.Web.Mvc.Html
    {
        public static class MyRenderHelpers
        {
            public static MvcHtmlString Html(this HtmlHelper helper, string html, bool condition)
            {
                if (condition)
                    return MvcHtmlString.Create(html);
                else
                    return MvcHtmlString.Empty;
            }
        }
    }
    

    Now you can use this extension method in your razor view:

    @Html.Html("
    ", somecondition)

提交回复
热议问题