Pass Html String from Controller to View ASP.Net MVC

53,091

Solution 1

In your controller action :

ViewBag.HtmlStr = "<table style=\"width:300px\"><tr><td>Jill</td><td>Smith</td> <td>50</td></tr><tr><td>Eve</td><td>Jackson</td><td>94</td></tr></table>";

Your view :

@Html.Raw(ViewBag.HtmlStr)

Solution 2

You can assign the html in controller to ViewBag and access the ViewBag in View to get the value that is the html

Controller

ViewBag.YourHTML = htmlString;

View

<div> @ViewBag.YourHTML </div>

Its better to not to pass the html from controller to View rather pass the object or collection of object to View (strongly typed view) and render html in View as it is responsibility of View

Controller

public ActionResult YourView()
{
    //YourCode
    return View(entities.yourCollection.ToList());
}

Veiw

<table style="width:300px">
    foreach (var yourObject in Model)
    {
       <tr>
             <td>@yourObject.FirstName</td>
             <td>@yourObject.LasttName</td> 
             <td>@yourObject.Amount</td>
       </tr>      
    }

</table>

Solution 3

Best approach will be to create a partial view and append the html returned by it in your parent view inside some container div.

In your main view do like this:

<div>

    @{

    Html.RenderAction("youraction","yourcontroller")

     }

</div>

in your action do this:

public ActionResult youraction()
{
 return View();
}

and your partial view:

@{
Layout = null;
}

<table style="width:300px">
<tr>
  <td>Jill</td>
  <td>Smith</td> 
  <td>50</td>
</tr>
<tr>
  <td>Eve</td>
  <td>Jackson</td> 
  <td>94</td>
</tr>
</table>

Solution 4

Controller

 ViewBag.HTMLData = HttpUtility.HtmlEncode(htmlString);  

View

@HttpUtility.HtmlEncode(ViewBag.HTMLData)

Solution 5

You can use ViewBag.YourField or ViewData["YourStringName"] and for retreiving it in your View. Just place it where you want preceded by @ like this @ViewBag.YourField or @ViewData["YourStringName"].

Share:
53,091

Related videos on Youtube

Pratik Bhoir
Author by

Pratik Bhoir

Hello

Updated on July 09, 2022

Comments

  • Pratik Bhoir
    Pratik Bhoir almost 2 years

    Which is the best way to pass the Html String block from Controller to View in MVC. I want it display that html block at the page load. Thank you. It can be any Html, e.g

    <table style="width:300px">
    <tr>
      <td>Jill</td>
      <td>Smith</td> 
      <td>50</td>
    </tr>
    <tr>
      <td>Eve</td>
      <td>Jackson</td> 
      <td>94</td>
    </tr>
    </table>
    

    I want to pass this as a string from controller to View. where it will be displayed as an html. Thank you.

  • Pratik Bhoir
    Pratik Bhoir about 10 years
    and how should i display it in view
  • Adil
    Adil about 10 years
    Yes, check my answer.
  • Sandman
    Sandman about 10 years
    Good answer Adil, but I really dislike the practice of creating html in a controller. Thats what views are for :)
  • Pratik Bhoir
    Pratik Bhoir about 10 years
    No Dude, this will only show that Html as a normal string. I want to display it as an Html
  • Adil
    Adil about 10 years
    Well it depends, its better to use the stongly typed views
  • Pratik Bhoir
    Pratik Bhoir about 10 years
    @Sandman You are right bro, But sometimes you are stuck to do so.. Cant help.
  • Sandman
    Sandman about 10 years
    @Pratik I feel your pain :)
  • Pratik Bhoir
    Pratik Bhoir about 10 years
    my html which is going to come is not static, its totally dynamic. so i cant create a fixed partial view.
  • Ehsan Sajjad
    Ehsan Sajjad about 10 years
    you can create dynamic html in partial view\
  • Uğur Aldanmaz
    Uğur Aldanmaz over 9 years
    Simple solution and simple is the best.
  • Pranesh Janarthanan
    Pranesh Janarthanan about 8 years
    helped me too .. super
  • Serj Sagan
    Serj Sagan about 8 years
    Be careful with Html.Raw() like this... a hacker could use this as a potential failure point...