Tuesday, November 11, 2014

Return a specific view and hand it a specific string as a model.

StringBuilder stringBuilder = new StringBuilder("Hello World!");
return View("Whatever", stringBuilder);

 
 

You may exclusively write the HTML for the model from the C# side (NOT RECOMMENDED) in an ASP.NET MVC Controller with this approach if your .cshtml view is kept this simple:

@model System.Text.StringBuilder
@{
   Layout = null;
}
@Html.Raw(Model.ToString())

 
 

As best as I can tell one does have to use the StringBuilder trick to use a string as a model or do something else to make the string not immediately a string type. If you just hand in a string to the View method it will be used in a different context in its method overloading.

No comments:

Post a Comment