Sections aren't supposed to be generated dynamically like that. What are you trying to do? If you want to render a section within the loop, you need to use RenderSection.
now in content page default.cshtml i pull data about widgets from db. i want to do foreach and fill all this sections with content. For now i did it like that:
but that way i have to repeat the same code for all sections, so that is why i wanted to loop thru all sections like in oryginal post and then do another loop thru rows that will call @RednerPage
Sections aren't supposed to be generated dynamically like that
My section are defined in _layout page like @RenderSection("section_name", false)
Mikesdotnetting
What are you trying to do?
in content page i try to loop thru records from db and for every row i try to insert content of that row to specific section. there are many sections and every section can have multiple rows in db.
Anyway THE BIG QUESTION IS : can i use @section section_name{} anywhere within the code ??? simple example:
@if (condition_here) {
@section section_name{}
}
will create error: Unexpected "section" keyword after "@" character. Once inside code, you do not
need to prefix constructs like "section" with "@".
is that the Razor parser thinks your section is named the literal "mySection". A @section block gets parsed as a call to DefineSection method. So you could effectively write:-
@foreach (var item in widgets) {
var row = item;
var sectionName = row.sName;
DefineSection{sectionName, () => {
Write(RenderPage("~/Templates/_" + row.wFile + ".cshtml", row));
});
}
This is not entirely nice because you cannot use Razor code inside the lambda (notice that I had to use the Write method instead of using "@" to print. You could work around this by declaring a template, but it doesn't look very elegant:-
cgs120
Member
103 Points
56 Posts
@section in foreach
Jun 23, 2011 05:09 PM|LINK
How can use
@section section_name{
}
in foreach loop ???
here is simple example of what i would like to do:
@foreach (var row in widgets)
{
var sectionName = row.sName;
@section sectionName{
}
}
}
and the error:
Unexpected "section" keyword after "@" character. Once inside code, you do not need to prefix constructs like "section" with "@".
Mikesdotnett...
All-Star
154955 Points
19872 Posts
Moderator
MVP
Re: @section in foreach
Jun 23, 2011 07:17 PM|LINK
Sections aren't supposed to be generated dynamically like that. What are you trying to do? If you want to render a section within the loop, you need to use RenderSection.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
cgs120
Member
103 Points
56 Posts
Re: @section in foreach
Jun 23, 2011 09:22 PM|LINK
OK here is what i have :
in _SiteLayout.cshtml page:
@RenderSection("rcHeader",false) @RenderSection("rcTop",false) @RenderSection("rcLeft",false) @RenderBody() @RenderSection("rcRight",false) @RenderSection("rcBottom",false) @RenderSection("rcFooter",false)now in content page default.cshtml i pull data about widgets from db. i want to do foreach and fill all this sections with content. For now i did it like that:
@section rcHeader{ @foreach (var row in widgets) { if (row.sName == "rcHeader") { @RenderPage("~/Templates/_" + row.wFile + ".cshtml", row); } } }@section rcTop{ @foreach (var row in widgets) { if (row.sName == "rcTop") { @RenderPage("~/Templates/_" + row.wFile + ".cshtml", row); } } }but that way i have to repeat the same code for all sections, so that is why i wanted to loop thru all sections like in oryginal post and then do another loop thru rows that will call @RednerPage
cgs120
Member
103 Points
56 Posts
Re: @section in foreach
Jun 27, 2011 01:26 PM|LINK
Any suggestions how to do this better ?
cgs120
Member
103 Points
56 Posts
Re: @section in foreach
Jun 30, 2011 03:55 PM|LINK
My section are defined in _layout page like @RenderSection("section_name", false)
in content page i try to loop thru records from db and for every row i try to insert content of that row to specific section. there are many sections and every section can have multiple rows in db.
Anyway THE BIG QUESTION IS : can i use @section section_name{} anywhere within the code ??? simple example:
@if (condition_here) { @section section_name{} }will create error: Unexpected "section" keyword after "@" character. Once inside code, you do not
need to prefix constructs like "section" with "@".
XiaoCheng Fa...
All-Star
17743 Points
1414 Posts
Re: @section in foreach
Jul 05, 2011 12:46 PM|LINK
Hi,
As far as I know, the block in @{} requires C# code and section is not part of reserved word, so you can not use section in it directly.
You can try to look for section class of WebMatrix.
I hope this can be helpful for you.
If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework
pranavkm
Participant
796 Points
106 Posts
Re: @section in foreach
Jul 05, 2011 07:15 PM|LINK
The problem with doing this -
var mySection = "Foo"; @section mySection { }is that the Razor parser thinks your section is named the literal "mySection". A @section block gets parsed as a call to DefineSection method. So you could effectively write:-
@foreach (var item in widgets) { var row = item; var sectionName = row.sName; DefineSection{sectionName, () => { Write(RenderPage("~/Templates/_" + row.wFile + ".cshtml", row)); }); }This is not entirely nice because you cannot use Razor code inside the lambda (notice that I had to use the Write method instead of using "@" to print. You could work around this by declaring a template, but it doesn't look very elegant:-
@foreach (var item in widgets) { var row = item; DefineSection(row, () => { Func<object, HelperResult> content = @<text> @RenderPage("~/Templates/_" + row.wFile + ".cshtml", row) Write(content(null)); </text>; }); }cgs120
Member
103 Points
56 Posts
Re: @section in foreach
Jul 05, 2011 09:06 PM|LINK
Thank you, here is what i use:
@foreach (var item in widgets) { var row = item; var sectionName = row.sName; DefineSection(sectionName, () => { Write(RenderPage("~/Templates/_" + row.wFile + ".cshtml", row)); }); }"Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type"
pranavkm
Participant
796 Points
106 Posts
Re: @section in foreach
Jul 05, 2011 09:46 PM|LINK
I'm not sure if there's a better way of dealing with this, but casting should resolve this.
@foreach (var widget in widgets) { var row = widget; var sectionName = (string)row.sName; var wFile = (string)row.wFile; DefineSection(sectionName, () => { Write(RenderPage("~/Templates/_" + wFile + ".cshtml", row)); }); }Also if you intend on using row in the sub-page being called, cast it to DynamicRecord first.
cgs120
Member
103 Points
56 Posts
Re: @section in foreach
Jul 05, 2011 10:06 PM|LINK
Perfect - thank you so much for your help. It will make my code so much shorter and easier to read/ change.
I'm very new to C# , so things like that are not so easy for me :) but i'm reading ...