For some time I have been using the deprecated overloads of the GetGridHtml IPublishedContent extension. I was just wondering how I would change it to the correct one, that needs a HtmlHelper as parameter!?
I'm using it in a data model, to ready the data for the view. Today it looks something like this:
using System.Web.Mvc;
using Umbraco.Core.Models;
using Umbraco.Web;
using Website.Extensions.Models.DocumentTypes.General;
namespace Website.Extensions.Models.DocumentTypes {
public class Page : ATemplateViewModel {
public string Headline { get; private set; }
public string Text { get; private set; }
public MvcHtmlString Grid { get; private set; }
public Page( IPublishedContent content )
: base( content ) {
Grid = content.GetGridHtml( "grid" );
Headline = content.GetPropertyValue
GetGridHtml i deprecated and Visual Studio tells me that its parameters should be:
GetGridHtml(HtmlHelper, string)
How would I do this here? The "Page" class will be instatiated in a controller, so I don't have the automatically whatever generated for the views.
From a MVC perspective, you normally wouldn't generate the HTML in the model. So given your current model, I would remove the Grid property, and then add a new one with the IPublishedContent (so it can be accessed from the view):
public IPublishedContent Content { get; private set; }
And then of course also set it in your constructor.
With the Content property present, you can then do something like this in your view (where Html is already present):
@Model.Content.GetGridHtml(Html, "grid")
The line below gives the same result (just another way to do it):
I see your point and I would never generate html in my model (Unless desperate). In this case though I'm not desperate, but I want a clean view, with NO knowledge of the IPublishedContent. I'm just filling my model properties with the data from the IPublishedContent object.
That's why I'm trying to figure it out. How the h*ll to make a HtmlHelper in my model?! :D
I don't see why that should even be necessary.
private static HtmlHelper CreateHtmlHelper(object model)
{
var cc = new ControllerContext
{
RequestContext = UmbracoContext.Current.HttpContext.Request.RequestContext
};
var viewContext = new ViewContext(cc, new FakeView(), new ViewDataDictionary(model), new TempDataDictionary(), new StringWriter());
var htmlHelper = new HtmlHelper(viewContext, new ViewPage());
return htmlHelper;
}
But warns like this:
"This shouldn't need to be used but because the obsolete extension methods above don't have access to the current HtmlHelper, we need to create a fake one, unfortunately however this will not pertain the current views viewdata, tempdata or model state so should not be used"
I guess I could just do it like that?! I don't need the current stuff.
Yes, you can create a fake HtmlHelper. I was just about to find some examples for that ;) But I normally don't go down this path, so I can't tell if there are any consequences by doing so.
Alternatively you can have a look at my Grid package. The main purpose of the package is to give you a strongly typed model for the grid - so rather than your Grid property being of the type MvcHtmlString, it could instead be of the type GridDataModel, you can then use in your view with full intellisense. But not knowing your project, this could involve some further work - so it's just a suggestion ;)
Thanks. I'm using Doc type grid editor in my grid, and creates strongly types objects based on the content delivered by that. So that part is covered. :)
How to call GetGridHtml with HtmlHelper?
Hi Guys,
For some time I have been using the deprecated overloads of the GetGridHtml IPublishedContent extension. I was just wondering how I would change it to the correct one, that needs a HtmlHelper as parameter!?
I'm using it in a data model, to ready the data for the view. Today it looks something like this:
GetGridHtml i deprecated and Visual Studio tells me that its parameters should be: GetGridHtml(HtmlHelper, string)
How would I do this here? The "Page" class will be instatiated in a controller, so I don't have the automatically whatever generated for the views.
Cheers Rune
From a MVC perspective, you normally wouldn't generate the HTML in the model. So given your current model, I would remove the
Grid
property, and then add a new one with the IPublishedContent (so it can be accessed from the view):And then of course also set it in your constructor.
With the
Content
property present, you can then do something like this in your view (whereHtml
is already present):The line below gives the same result (just another way to do it):
Hi Anders,
I see your point and I would never generate html in my model (Unless desperate). In this case though I'm not desperate, but I want a clean view, with NO knowledge of the IPublishedContent. I'm just filling my model properties with the data from the IPublishedContent object.
That's why I'm trying to figure it out. How the h*ll to make a HtmlHelper in my model?! :D I don't see why that should even be necessary.
/Rune
Looks like Umbraco does it like this
But warns like this: "This shouldn't need to be used but because the obsolete extension methods above don't have access to the current HtmlHelper, we need to create a fake one, unfortunately however this will not pertain the current views viewdata, tempdata or model state so should not be used"
I guess I could just do it like that?! I don't need the current stuff.
/Rune
Yes, you can create a fake
HtmlHelper
. I was just about to find some examples for that ;) But I normally don't go down this path, so I can't tell if there are any consequences by doing so.Alternatively you can have a look at my Grid package. The main purpose of the package is to give you a strongly typed model for the grid - so rather than your
Grid
property being of the typeMvcHtmlString
, it could instead be of the typeGridDataModel
, you can then use in your view with full intellisense. But not knowing your project, this could involve some further work - so it's just a suggestion ;)Thanks. I'm using Doc type grid editor in my grid, and creates strongly types objects based on the content delivered by that. So that part is covered. :)
Hi, Rune,
You've probably seen it, but this issue has some very helpful comments and a couple links that may help.
http://issues.umbraco.org/issue/U4-6209
cheers,
doug.
Thanks Doug
No I had not found that. Looks like they are planning to mess up my code even more :)
I will take the discution there. There must be another way!
/Rune
is working on a reply...