Click here to Skip to main content
15,891,691 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public ActionResult index()
       {
           return View("<div>hello</div>");
       }


My Layout-

HTML
<html>
<head>
</head>

<body>
  <h1>Hello world</h1>
  @Render.body();
</body>

</html>


I want to see:

Hello World
hello
Posted

1 solution

You could use the following in your view for an index action:
HTML
<!-- 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:
C#
public ActionResult Index()
{
    return View(new HtmlString("<div>hello</div>"));
}

HTML
@model HtmlString

@Model
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900