如何从控制器发送Html标签到查看 [英] How Do I Send Html Tags From Controller To View
本文介绍了如何从控制器发送Html标签到查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public ActionResult index()
{
return 查看( < div> hello< / div>);
}
我的布局 -
< html >
< head >
< / head >
< body >
< h1 < span class =code-keyword>> Hello world < / h1 < span class =code-keyword>>
@ Render.body();
< / body >
< / html >
我想看:
Hello World
hello
解决方案
您可以在视图中使用以下内容进行索引操作:
<! - 定义模型类型 - >
@model string
< span class =code-keyword><! - 将模型渲染为原始HTML内容。 - >
@ Html.Raw(型号)
但是我相信这会导致调用 index 操作时出现问题,因为string
模型会导致路径模糊。
作为解决方法,您可以将此字符串
转换为对象
,然后将其传递给 View 然后在模型
上调用ToString
。
但更好的方法是使用以下内容:
public ActionResult Index( )
{
return 查看( new HtmlString( < div> hello< / div>));
}
@ model HtmlString
@Model
public ActionResult index()
{
return View("<div>hello</div>");
}
My Layout-
<html>
<head>
</head>
<body>
<h1>Hello world</h1>
@Render.body();
</body>
</html>
I want to see:
Hello World
hello
解决方案
You could use the following in your view for an index action:
<!-- Define model type --> @model string <!-- Render model as a raw HTML content. --> @Html.Raw(Model)
However I believe this should cause an issue when invoking that index action because astring
model is causing the path's ambiguity.
As a workaround to this you could cast thisstring
as anobject
when passing it to View and then callToString
on yourModel
.
But a better way would be to just use the following:
public ActionResult Index() { return View(new HtmlString("<div>hello</div>")); }
@model HtmlString @Model
这篇关于如何从控制器发送Html标签到查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文