I'm trying to figure out how to code a strongly-typed partial view (the new macro partial). Somewhere down the road, I would like to have something like this available:
In other words, I would like to fully utilize the MVC structure. And in a model-view-controller composition, the logic is placed in the model, and the controller makes sure you get what you asked for.
So you have your partialview say which at the top will have:
YOUR MODEL >> this will be in a class liabary somewhere that is built into your www root bin. This could for instance have property IENUM of all your node properties.
YOUR SUFACE CONTROLLER >> this will call your method to get the IENUM and return it to the partial view
YOUR VIEW >> this will create a new instance of the model and populate the model IENUM property
YOUR PARTIAL VIEW >> at the top of this you will have something like:
@model YOURNAMESPACE.YOURMODEL
and then
foreach(item in YOURMODEL.YOUR PROPERTY)
{
logic
}
Is this what you were you after? Please let me know if you need some more help :) Charlie
Hi, not totally sure what you have done? could you explain it? How is your surface controller working? It does not appear to be going anywhere? Charlie :)
Strongly-Typed Partial View Macro in 6.xx?
Hello!
I have a small setup with;
Yep you can do that.
So you have your partialview say which at the top will have:
YOUR MODEL >> this will be in a class liabary somewhere that is built into your www root bin. This could for instance have property IENUM of all your node properties.
YOUR SUFACE CONTROLLER >> this will call your method to get the IENUM and return it to the partial view
YOUR VIEW >> this will create a new instance of the model and populate the model IENUM property
YOUR PARTIAL VIEW >> at the top of this you will have something like:
@model YOURNAMESPACE.YOURMODEL
and then
foreach(item in YOURMODEL.YOUR PROPERTY)
{
logic
}
Is this what you were you after? Please let me know if you need some more help :) Charlie
Hi Charlie,
I might need further help to understand what you mean :-)
I already have a class 'Footer', which inherits from 'RenderModel'. My model has this property:
public string FooterText { get; set; }
I want to populate that with the real text ofc ;) But I don't know what you mean by "SUFACE CONTROLLER"...
You can find the documentation of surface controllers here : http://our.umbraco.org/documentation/Reference/Mvc/surface-controllers
Dave
Hi Kristian :)
Here is some information about SurfaceControllers and MVC with Umbraco
http://our.umbraco.org/documentation/Reference/Mvc/surface-controllers
http://our.umbraco.org/documentation/Reference/Mvc
It is just away of using a controller in the context of an umbraco application.
Say you have a FooterModel:
using umbraco.NodeFactory;
namespace
{
public class FooterModel
{
//property
public string FooterText {get;set;}
//const of the property alias in Umbraco
const string FOOTERPROPERTYALIAS = "footertext";
//some method to populateproperty
public string FooterText()
{
//get contextnode
Node contextNode = Node.GetCurrent();
//assign property from umbraco to public property or use the default if nothing is there
FooterText = String.IsNullOrEmpty(contextNode .GetProperty(FOOTERPROPERTYALIAS ).Value.ToString()) ? "defaultifempty" : contextNode .GetProperty(FOOTERPROPERTYALIAS ).Value.ToString()
}
}
}
Then (i presume your HTML view) the place where you call renderBody()
call the partial and pass in a new instance of your footer model
@Html.RenderPartial("Footer", new FooterModel());
//FOOTER PARTIAL VIEW "FOOTER"
@modelyournamespace.FooterModel
@{
Page,Title = "something";
}
<h1>@Model.FooterText</h1>
Does that make sense? :)
Thanks for your replies!
However, I have to make sure you know I mean 'Partial View Macro' and not the ordinary 'Partial View' from Umbraco 4+.
I contintued to read about surface controllers and found this article http://www.proworks.com/blog/2013/02/07/umbraco-6-what's-new/
So I setup my FooterSurfaceController:
public class FooterController : SurfaceController
{
public PartialViewResult Footer()
{
Footer footerModel = new Footer();
return PartialView(footerModel);
}
}
And kept my Footer model as it was, implementing the logic needed in its constructor.
My Footer Partial Macro View is then defined like this:
@{
Layout = null;
}
@model namespace.Footer
<div class="footer">
@Html.Raw(Model.FooterText)
</div>
And this is much cleaner than before. Now my view does its only job - to present data, not retrieve AND present.
Hi, not totally sure what you have done? could you explain it? How is your surface controller working? It does not appear to be going anywhere? Charlie :)
is working on a reply...