I have simple CSHTML page with this code. Why "foreach" doesn't generate anything? I have this problem also in another situations - can't generate html from within function.
@functions{voidWriteArticles(){vardb=Database.Open("Data");varselectQueryString="SELECT * FROM Articles";vardata=db.Query(selectQueryString);foreach(varrowindata){Html.Raw("<p>"+row.Name+"</p>");}}} @{ WriteArticles(); }
@helper WriteArticles() {
var db = Database.Open("Data");
var selectQueryString = "SELECT * FROM Articles";
var data = db.Query(selectQueryString);
foreach(var row in data){
<p>@row.Name</p>
}
}
@WriteArticles()
vklaus
Member
113 Points
74 Posts
How to generate HTML from void()
Dec 03, 2012 05:27 PM|LINK
Hello,
I have simple CSHTML page with this code. Why "foreach" doesn't generate anything? I have this problem also in another situations - can't generate html from within function.
Vladimir
Mikesdotnett...
All-Star
154905 Points
19866 Posts
Moderator
MVP
Re: How to generate HTML from void()
Dec 03, 2012 07:10 PM|LINK
You should use a helper, not a function to render HTML: http://www.mikesdotnetting.com/Article/173/The-Difference-Between-@Helpers-and-@Functions-In-WebMatrix
@helper WriteArticles() { var db = Database.Open("Data"); var selectQueryString = "SELECT * FROM Articles"; var data = db.Query(selectQueryString); foreach(var row in data){ <p>@row.Name</p> } } @WriteArticles()Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
vklaus
Member
113 Points
74 Posts
Re: How to generate HTML from void()
Dec 04, 2012 05:47 AM|LINK
Thanks Mike, as usual - excelent answer as well as article!
Vladimir