I'm having trouble returning a custom model, which inherits from a RenderModel, to a UmbracoViewPage.
The following error occurs
Cannot bind source type BD.Web.Models.SiteFilterModel to model type BD.Web.Models.SiteFilterModel.
I need to return the model to the view to use it in a filter.
Could someone point out what i'm doing wrong? I'm still pretty new to Umbraco, so i'm still trying to figure somethings out.
Controller:
public class IndexController : Umbraco.Web.Mvc.RenderMvcController
{
public FilterEnumerations.SiteFilter SiteFilter
{
get {
if (Session["SiteFilter"] == null)
return FilterEnumerations.SiteFilter.AllSites;
return (FilterEnumerations.SiteFilter)Enum.Parse(typeof(FilterEnumerations.SiteFilter), Session["SiteFilter"].ToString());
}
}
// GET: Index
public ActionResult Index(Umbraco.Web.Models.RenderModel model)
{
var siteFilterModel = new SiteFilterModel(CurrentPage)
{
SiteFilter = SiteFilter
};
return CurrentTemplate(siteFilterModel);
}
}
var siteFilterModel = new SiteFilterModel(CurrentPage)
{
SiteFilter = SiteFilter
};
to
var siteFilterModel = new SiteFilterModel(model)
{
SiteFilter = SiteFilter
};
For the model i usually do this
public class SiteFilterModel : Umbraco.Web.Models.RenderModel
{
public SiteFilterModel(IPublishedContent content, CultureInfo culture) : base(content, culture)
{
}
public SiteFilterModel(IPublishedContent content) : base(content)
{
}
public FilterEnumerations.SiteFilter SiteFilter { get; set; }
}
The currentpage is an IpublishedContent discribing the node that umbraco links to your controller.
the Rendermodel contains Content (which is the same as currentpage) and culturedata as properties
First load of the page is now working fine. But now the error occurs after submitting the form. Do you have anymore ideas?
I've read in a topic that it's better to put something like this in a partialview and render it via @Html.Action. So i've rewritten the filter as a partial view and rendering it via a surfacecontroller, but that doesn't make any difference.
My code now looks like this.
Model:
public class SiteFilterModel : Umbraco.Web.Models.RenderModel
{
public SiteFilterModel() : base(Umbraco.Web.UmbracoContext.Current.PublishedContentRequest.PublishedContent) { }
public SiteFilterModel(IPublishedContent content) : base(content) {}
public SiteFilterModel(IPublishedContent content, CultureInfo culture) : base(content, culture) { }
public FilterEnumerations.SiteFilter SiteFilter { get; set; }
}
Surfacecontroller:
public class FilterController : Umbraco.Web.Mvc.SurfaceController
{
[ChildActionOnly]
public ActionResult SiteFilter()
{
var siteFilterModel = new SiteFilterModel();
var siteFilter = FilterEnumerations.SiteFilter.AllSites;
if (Session["SiteFilter"] != null)
{
siteFilter = (FilterEnumerations.SiteFilter)Enum.Parse(typeof(FilterEnumerations.SiteFilter), Session["SiteFilter"].ToString());
}
siteFilterModel.SiteFilter = siteFilter;
return PartialView("~/Views/Partials/Filter/SiteFilter.cshtml", siteFilterModel);
}
[HttpPost]
public ActionResult SiteFilterSubmit(SiteFilterModel model)
{
Session["SiteFilter"] = model.SiteFilter;
return RedirectToCurrentUmbracoPage();
}
The page itself inherits from the UmbracoViewPage, and renders the PartialView like this: @Html.Action("SiteFilter", "Filter")
The PartialView also inherits from the UmbracoViewPage but with a model. There is no layout or anything it, just a foreach loop and some controls.
Hey Raymond, I will try and get you a fully working example of an UmbracoMvcController with a custom model together with a surfaceController form later today.
Returning model to viewpage
I'm having trouble returning a custom model, which inherits from a RenderModel, to a UmbracoViewPage.
The following error occurs
I need to return the model to the view to use it in a filter.
Could someone point out what i'm doing wrong? I'm still pretty new to Umbraco, so i'm still trying to figure somethings out.
Controller:
View:
Model:
Thanks in advance.
In the controller, change
to
For the model i usually do this
The currentpage is an IpublishedContent discribing the node that umbraco links to your controller. the Rendermodel contains Content (which is the same as currentpage) and culturedata as properties
Sven thanks for answering my question.
I've changed the code in my model and controller to match yours, but unfortunatly i'm getting a different error now.
Am I missing something? Or should the controller be a surfacecontroller instead of a rendermvccontroller?
Oops, sorry my bad.
In the controller
var siteFilterModel = new SiteFilterModel(model)
tovar siteFilterModel = new SiteFilterModel(model.content)
First load of the page is now working fine. But now the error occurs after submitting the form. Do you have anymore ideas?
I've read in a topic that it's better to put something like this in a partialview and render it via @Html.Action. So i've rewritten the filter as a partial view and rendering it via a surfacecontroller, but that doesn't make any difference.
My code now looks like this.
Model:
Surfacecontroller:
The page itself inherits from the UmbracoViewPage, and renders the PartialView like this: @Html.Action("SiteFilter", "Filter")
The PartialView also inherits from the UmbracoViewPage but with a model. There is no layout or anything it, just a foreach loop and some controls.
Hey Raymond, I will try and get you a fully working example of an UmbracoMvcController with a custom model together with a surfaceController form later today.
Hey Sven, that would be great. Thanks in advance.
So, I have been busy as hell... and have not had the time to make a working example.
In the partial that gets rendered by the action, are you using
(Html.BeginUmbracoForm<ControllerName>("ActionName")
?Sorry it took me a to now to reply, working on another project...
No i'm using
@using (Html.BeginUmbracoForm("SiteFilterSubmit", "Filter", null, new { id = "form-sitesfilter" }, FormMethod.Post))
is working on a reply...