Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Raymond Rotgans 7 posts 75 karma points
    Jul 18, 2017 @ 14:05
    Raymond Rotgans
    0

    Returning model to viewpage

    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);
        }
    }
    

    View:

    @inherits UmbracoViewPage<BD.Web.Models.SiteFilterModel>
    @{
        Layout = "_ComparisonLayout.cshtml";
    }
    

    Model:

    public class SiteFilterModel : Umbraco.Web.Models.RenderModel
    {
        public SiteFilterModel() : base(Umbraco.Web.UmbracoContext.Current.PublishedContentRequest.PublishedContent) { }
        public SiteFilterModel(IPublishedContent content) : base(content) {}
    
    
        public FilterEnumerations.SiteFilter SiteFilter { get; set; }
    }
    

    Thanks in advance.

  • Sven Geusens 169 posts 881 karma points c-trib
    Jul 20, 2017 @ 15:30
    Sven Geusens
    0

    In the controller, change

    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

  • Raymond Rotgans 7 posts 75 karma points
    Jul 24, 2017 @ 06:37
    Raymond Rotgans
    0

    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.

    Cannot covert Umbraco.Web.Models.RenderModel to Umbraco.Core.Models.IPublishContent
    

    Am I missing something? Or should the controller be a surfacecontroller instead of a rendermvccontroller?

  • Sven Geusens 169 posts 881 karma points c-trib
    Jul 24, 2017 @ 10:15
    Sven Geusens
    0

    Oops, sorry my bad.

    In the controller var siteFilterModel = new SiteFilterModel(model) to var siteFilterModel = new SiteFilterModel(model.content)

  • Raymond Rotgans 7 posts 75 karma points
    Jul 24, 2017 @ 12:13
    Raymond Rotgans
    0

    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.

    @inherits UmbracoViewPage<BD.Web.Models.SiteFilterModel>
    
  • Sven Geusens 169 posts 881 karma points c-trib
    Jul 24, 2017 @ 12:16
    Sven Geusens
    0

    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.

  • Raymond Rotgans 7 posts 75 karma points
    Jul 24, 2017 @ 12:19
    Raymond Rotgans
    0

    Hey Sven, that would be great. Thanks in advance.

  • Sven Geusens 169 posts 881 karma points c-trib
    Jul 27, 2017 @ 08:24
    Sven Geusens
    0

    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")?

  • Raymond Rotgans 7 posts 75 karma points
    Jul 31, 2017 @ 05:49
    Raymond Rotgans
    0

    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))

Please Sign in or register to post replies

Write your reply to:

Draft