如何从控制器发送Html标签到查看 [英] How Do I Send Html Tags From Controller To View

查看:63
本文介绍了如何从控制器发送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 a string model is causing the path's ambiguity.
As a workaround to this you could cast this string as an object when passing it to View and then call ToString on your Model.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆