I have been looking into the
possibility of moving our site from web forms to MVC, however our current
project structure either doesn't lend itself to MVC or I am missing something.
In our current site we have a hierarchy along the lines of:
Etc. Most content nodes have a
corresponding template. Each template defines content place holders for its
children, and places content in for its parents. In this way the sections are combined onto a single page.
The problem with transferring
this to MVC is that you cannot link a content node to a template view that
contains a "RenderSection" or "RenderBody" method; it must
be a leaf node.
The only way I have found to
get around this is to use partial views but that has the serious drawback of
not linking the model automatically. This means you have to search out the
correct model in code, e.g.
I see your thinking, but it is just kinda upside-down, I mean MVC compared to webforms.
Each docType will have a template, this is now a View.
Create a _Layout.cshtml file, including <header>, @RenderBody(), <footer>. This is the only time you need the @RenderBody() statement, the _Layout is just a .cshtml file and has no docType.
Your View(s) then call @{Layout = "~/_layout.cshtml"} at the top and (View (docType template) is sucked into the _Layout at the point where the @RenderBody() appears.
So in Section 1 View, you can write directly @foreach (var item in Model.Children()) { item.YourProperty} and this will be called into the Section 1 View, which is inside the _Layout. and list your property for as many children as you have.
If you are in the View (template) for Section 1B and want a property from Section 1, @Model.Up().YourProperty is all you need.
Partial Views can be used for Navigation and any area where content is common, if you were to write a partial for Section 1B to get the property from Section 1. just call @Html.Partial("GetMyPropertyFromSection1") in your Section 1B View, then in the Partial, no layout is called you can put @Model.Up().YourProperty and it will work just the same.
My confusion with Webforms was that you had to think about the next page, while in the current page then match the now current page to the page you were in before (ugh!), but if I were to describe the Mvc way it could be, stay on the page where you are wanting to see the property, and go get it.
Trying to keep the explanation simple, without trying to sound condescending, but I did struggle with this for a while myself, a simple explanation without reams of code and complexity would have helped me then, so I hope this can help you.
Any questions, please ask, if you can get this bit cracked it becomes a joy after.
OK so to clarify you would push the content up into a single cshtml file, which would loop through all the child nodes properties? I can certainly operate in that manner it just makes migration more difficult, because it is a complete change in structuring style.
The thing I liked about umbraco webforms is that one content node mapped to one template, where as you are pointing to methods to get child and parent properties within a single view.
This also makes it more difficult to develop multiple areas at once, because rather than just define a template which can then be used by others you need to directly fit in any new functionality into one large existing file.
I understand in non umbraco terms the advantages that MVC brings about but since I am not taking advantage of any Controller hijacking, and the Models are the content tree nodes this actually seems to hinder the separation rather than help. I am sure there must be a better way, but essentially what I would like is a way of combining different View files that are connected to their own model.
Perhaps all this text is not helpful. A simple problem might be; what would you use, in MVC, to replace a nested another content place holder such as:
Well nothing is set in stone at the moment, there is a willingness to change over from higher ups in my company and I am merely investigating into the possibility. It is certainly not necessary and so far I would be recommending against it. Don't get me wrong there are some nice things in MVC, I particularly like having intellisense and I feel like the views have more control meaning less need to resort to using XSLT macros etc. Would you recommend against it?
However unless there is a specific need for something in MVC that you can't do in WebForms I'm not sure it's worth it. I think there's is an obvious and popular feeling that 'newer' is better etc. it would be interesting to know the business case to convert an Umbraco WebForms site to MVC.
Well I don't think there is anything that we couldn't do in webforms, it was much more a question of styling and layout. I guess I haven't quite got my head around the best practices for umbraco MVC yet and that is why I have not really seen the kind of benefits I expected to in this area.
It seems that most of the MVC examples I have looked at (e.g. http://our.umbraco.org/projects/starter-kits/standard-website-mvc) take the idea of having one view file access the model and then it is passed around to the other views. This seems rather backwards to me.
Migrating from Web forms to MVC
Hi all,
I have been looking into the possibility of moving our site from web forms to MVC, however our current project structure either doesn't lend itself to MVC or I am missing something. In our current site we have a hierarchy along the lines of:
Content
Website
Section 1
Section 1A
Section 1B
Section 2
Etc. Most content nodes have a corresponding template. Each template defines content place holders for its children, and places content in for its parents. In this way the sections are combined onto a single page.
The problem with transferring this to MVC is that you cannot link a content node to a template view that contains a "RenderSection" or "RenderBody" method; it must be a leaf node.
The only way I have found to get around this is to use partial views but that has the serious drawback of not linking the model automatically. This means you have to search out the correct model in code, e.g.
@Html.Partial("Section 1", @Model.Content.Descedents().First())
So my question is this, is there a way to link a view to both its parents/children and to its model in a single page hierarchical site?
If I didn't explain that well enough, let me know and I will try clarify. Thanks!
Hi John
I see your thinking, but it is just kinda upside-down, I mean MVC compared to webforms.
Each docType will have a template, this is now a View.
Create a _Layout.cshtml file, including <header>, @RenderBody(), <footer>. This is the only time you need the @RenderBody() statement, the _Layout is just a .cshtml file and has no docType.
Your View(s) then call @{Layout = "~/_layout.cshtml"} at the top and (View (docType template) is sucked into the _Layout at the point where the @RenderBody() appears.
So in Section 1 View, you can write directly @foreach (var item in Model.Children()) { item.YourProperty} and this will be called into the Section 1 View, which is inside the _Layout. and list your property for as many children as you have.
If you are in the View (template) for Section 1B and want a property from Section 1, @Model.Up().YourProperty is all you need.
Partial Views can be used for Navigation and any area where content is common, if you were to write a partial for Section 1B to get the property from Section 1. just call @Html.Partial("GetMyPropertyFromSection1") in your Section 1B View, then in the Partial, no layout is called you can put @Model.Up().YourProperty and it will work just the same.
My confusion with Webforms was that you had to think about the next page, while in the current page then match the now current page to the page you were in before (ugh!), but if I were to describe the Mvc way it could be, stay on the page where you are wanting to see the property, and go get it.
Trying to keep the explanation simple, without trying to sound condescending, but I did struggle with this for a while myself, a simple explanation without reams of code and complexity would have helped me then, so I hope this can help you.
Any questions, please ask, if you can get this bit cracked it becomes a joy after.
Regards
Gary
Hi Gary, thanks a lot for the reply!
OK so to clarify you would push the content up into a single cshtml file, which would loop through all the child nodes properties? I can certainly operate in that manner it just makes migration more difficult, because it is a complete change in structuring style.
The thing I liked about umbraco webforms is that one content node mapped to one template, where as you are pointing to methods to get child and parent properties within a single view.
This also makes it more difficult to develop multiple areas at once, because rather than just define a template which can then be used by others you need to directly fit in any new functionality into one large existing file.
I understand in non umbraco terms the advantages that MVC brings about but since I am not taking advantage of any Controller hijacking, and the Models are the content tree nodes this actually seems to hinder the separation rather than help. I am sure there must be a better way, but essentially what I would like is a way of combining different View files that are connected to their own model.
Perhaps all this text is not helpful. A simple problem might be; what would you use, in MVC, to replace a nested another content place holder such as:
“<asp:Content ContentPlaceHolderID="section1_content" runat="server">
<.../>
<asp:contentplaceholder id="section1A_content" runat="server"/>
<…/>
</asp:Content>”
Thanks again,
John.
Hi John,
What's your reason for wanting to migrate?
Rich
Hi Rich,
Well nothing is set in stone at the moment, there is a willingness to change over from higher ups in my company and I am merely investigating into the possibility. It is certainly not necessary and so far I would be recommending against it. Don't get me wrong there are some nice things in MVC, I particularly like having intellisense and I feel like the views have more control meaning less need to resort to using XSLT macros etc. Would you recommend against it?
Thanks,
John.
Hey John,
I wouldn't recommend for or against it really.
However unless there is a specific need for something in MVC that you can't do in WebForms I'm not sure it's worth it. I think there's is an obvious and popular feeling that 'newer' is better etc. it would be interesting to know the business case to convert an Umbraco WebForms site to MVC.
Rich
Hey Rich,
Well I don't think there is anything that we couldn't do in webforms, it was much more a question of styling and layout. I guess I haven't quite got my head around the best practices for umbraco MVC yet and that is why I have not really seen the kind of benefits I expected to in this area.
It seems that most of the MVC examples I have looked at (e.g. http://our.umbraco.org/projects/starter-kits/standard-website-mvc) take the idea of having one view file access the model and then it is passed around to the other views. This seems rather backwards to me.
Thanks for the advice anyway,
John.
is working on a reply...