ASP.NET MVC 3 using Razor - use conditional expression together with the HTML output

安稳与你 提交于 2019-12-30 18:06:09

问题


I've been bugged with this for many days...(I'm in process of studying ASP.NET MVC 3)

In this link -- Razor If/Else conditional operator syntax -- is said that the only valid syntax for conditional expression in Razor engine is @(x?y:z)

Alright. Now, how do I write HTML in this conditional expression? I can't use Razor here, following code results in invalid syntax error.

@(item.Manager == null ? @:<i>unassigned</i> : item.Manager.Name)

After some research I discovered HtmlWriter or Html.Raw but neither of them, nor their methods .toString() or .toHtmlString() help because they are not of string but of IHtmlString type.

Thanks for reply!


回答1:


@(item.Manager == null ? new HtmlString("<i>unassigned</i>") : new HtmlString( item.Manager.Name) )


来源:https://stackoverflow.com/questions/6762537/asp-net-mvc-3-using-razor-use-conditional-expression-together-with-the-html-ou

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!