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.
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:
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
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:
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>
<img src="slider/background/plainwhite.jpg" alt="" />
</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?
Member
17 Points
56 Posts
Storing HTML in SQL and posting to page
Aug 11, 2013 03:38 PM|happysack|LINK
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?
All-Star
194847 Points
28099 Posts
Moderator
Re: Storing HTML in SQL and posting to page
Aug 11, 2013 03:56 PM|Mikesdotnetting|LINK
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)
Member
17 Points
56 Posts
Re: Storing HTML in SQL and posting to page
Aug 11, 2013 04:39 PM|happysack|LINK
Contributor
4010 Points
1926 Posts
Re: Storing HTML in SQL and posting to page
Aug 11, 2013 06:58 PM|Afzaal.Ahmad.Zeeshan|LINK
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:
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:
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:
It will convert data to String, its helpfull while writing the data from INT datatype to string. Or while comparing them as:
I hope its enough to understand!
And as Mike said: Let us have a look at your code.
Member
17 Points
56 Posts
Re: Storing HTML in SQL and posting to page
Aug 12, 2013 03:40 AM|happysack|LINK
Ok..
My code isn't 'exactly' the same as that, but works in the same way.
So currently. My code reads...
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!
Member
17 Points
56 Posts
Re: Storing HTML in SQL and posting to page
Aug 12, 2013 03:44 AM|happysack|LINK
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..
This is currently displaying correctly.
Many thanks.
All-Star
194847 Points
28099 Posts
Moderator
Re: Storing HTML in SQL and posting to page
Aug 12, 2013 05:47 AM|Mikesdotnetting|LINK
Yes. That is how you use Html.Raw to render HTML unencoded so that it behaves like HTML.
Contributor
4010 Points
1926 Posts
Re: Storing HTML in SQL and posting to page
Aug 12, 2013 06:06 AM|Afzaal.Ahmad.Zeeshan|LINK
This is not the correct way to do it!
You might need to do something like:
Where the background column, stores the image name to be shown. You can upload the image and save the name here!