locked
Storing HTML in SQL and posting to page RRS feed

  • Question

  • User-601888530 posted

    Ok so..

    Using the Sql query command then 'for each var in row' bit found in this tutorial

    http://www.microsoft.com/web/post/web-development-101-part-5-using-data

    I have a different SQL command now that uses 'Left Join' which works and create a string of HTML of what i want though when i call in in my page it just displays the HTML and breaks it a bit rather than showing what the code should do.

    hope this makes sense! any ideas?

    Sunday, August 11, 2013 3:38 PM

Answers

  • User-821857111 posted

    It woudl be useful if you actually showed the code that causes you problems. In the meantime, if you want to render HTML, you should output it using the Html.Raw helper:

    @Html.Raw(myHtml)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 11, 2013 3:56 PM
  • User-601888530 posted

    Ok so this seems to be working...

    Could you verify wether this is the correct way to code?

    My head is the same but in the body i now have..

    <div>
    <ul>
     @foreach(var row in allfirstpositionproducts)   
                        {
                            <li>
                            @Html.Raw(@row.background)
    </li>
    }
    </ul>
    </div>
    
    


    This is currently displaying correctly.

    Many thanks.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 12, 2013 3:44 AM

All replies

  • User-821857111 posted

    It woudl be useful if you actually showed the code that causes you problems. In the meantime, if you want to render HTML, you should output it using the Html.Raw helper:

    @Html.Raw(myHtml)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 11, 2013 3:56 PM
  • User-601888530 posted
    I have linked to the 101 tutorial my code currently looks like that. Rather than having movie information in my db I have code. To use that helper then. In regards to that code .. I currently have <ol> @foreach(var row in data) { <li><a href="#">@row.Name</a></li> } </ol> Would I change that to.. <ol> @foreach(var row in data) { <li><a href="#">@HTML.row(Name)</a></li> } </ol> Would that work? Not totally upto scratch with helpers just learning this
    Sunday, August 11, 2013 4:39 PM
  • User895691971 posted

    Wait a minute, why are you storing the name of the movie as an HTML element?

    Why can't you just simply write the Name in it.

    However for me. Storing the HTML to the database won't be a better idea. I would prefer something like:

    // in the body element..
    <div class="name">@row.Name</div>
    </div class="duration">@row.TotalTime</div>
    <div class="otherInfo">@row.Details</div>
    
    // in the database.
    Name: nvarchar[max(4000)] 
    duration: nvarchar[max(4000)] // you can use any int for this data! for any data..
    otherInfo: nvarchar[max(40000]
    
    // apart from this I will have something like: an ID. That will be
    MovieID: int[primary(check); identity(check)]

    This way I will save the data, and will extract it and show it to the user!

    And you said: The SQL clause is producing HTML string.

    1. SQL Don't do that!

    2. SQL Produces something like: 

    System.Data.SqlServerCe
    
    // or something else like that! 

    That even I don't fully know. Mike would help you out in this, how to understand System.*

    The data you retrieve from Database will be shown the way you want it to. To make sure that its always a string. Write this: 

    @row.Name.ToString();

    It will convert data to String, its helpfull while writing the data from INT datatype to string. Or while comparing them as: 

    if(row.MovieID.ToString() == "2") { // conversion to string
      // code
    }

    I hope its enough to understand!

    And as Mike said: Let us have a look at your code.

    Sunday, August 11, 2013 6:58 PM
  • User-601888530 posted

    Ok..

    My code isn't 'exactly' the same as that, but works in the same way.

    So currently. My code reads...

    //HEAD
    
    @{
           var db = Database.Open("firstdatabase");
           var sqlQ = "SELECT * FROM firsttable RIGHT JOIN slideroptions ON firsttable.layout=slideroptions.id";
           var allfirsttableproducts = db.Query(sqlQ); 
    }
    
    
    //BODY
    
    <div class="fullwidthbanner">
                		<ul>
    
                        @foreach(var row in allfirsttableproducts)   
                        {
                            <li>
                            @row.background.ToString()
                            </li>
    
    }
    </ul>
    </div>
    
    // When i run the page and view the source code the area that that is posting reads like the following
    
    <div class="fullwidthbanner">
                		<ul>
    
                            <li>
                            &lt;img src=&quot;slider/background/plainwhite.jpg&quot; alt=&quot;&quot; /&gt;
                            </li>
                            
                            </ul>    
    </div>
    

    Can i put each one to a readable HTML string, but inside the loop?

    I have a slider of products which i want to be able to amend from inside a CMS.

    Depending on templates chosen from within this cms it should apply different html (from the left join)

    Hope this make sense, can't believe its that hard to display HTML from db?

    Thanks for your help so far!

    Monday, August 12, 2013 3:40 AM
  • User-601888530 posted

    Ok so this seems to be working...

    Could you verify wether this is the correct way to code?

    My head is the same but in the body i now have..

    <div>
    <ul>
     @foreach(var row in allfirstpositionproducts)   
                        {
                            <li>
                            @Html.Raw(@row.background)
    </li>
    }
    </ul>
    </div>
    
    


    This is currently displaying correctly.

    Many thanks.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 12, 2013 3:44 AM
  • User-821857111 posted

    happysack

    Could you verify wether this is the correct way to code?

    Yes. That is how you use Html.Raw to render HTML unencoded so that it behaves like HTML.

    Monday, August 12, 2013 5:47 AM
  • User895691971 posted

    This is not the correct way to do it!

    You might need to do something like: 

    <img src="~/files/@row.Background" alt="photo" />

    Where the background column, stores the image name to be shown. You can upload the image and save the name here! 

    Monday, August 12, 2013 6:06 AM