Copied to clipboard

Flag this post as spam?

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


  • Rabea 34 posts 186 karma points
    Nov 26, 2018 @ 07:05
    Rabea
    0

    Model mismatch error

    Hi, I am getting the following error when trying to add a partial view to a main view. The main view has a model generated by modelsbuilder and the partial view has an archetype model created by us. Is there a way to fix this?

    Cannot bind source type Namespace.Core.Models.Archetype Models.Text With Picture to model type Umbraco.Core.Models.IPublishedContent

  • Nik 1612 posts 7258 karma points MVP 7x c-trib
    Nov 26, 2018 @ 10:41
    Nik
    0

    Hi Rabea,

    How are you calling your partial view? And what does your partial view look like?

    Thanks,

    Nik

  • Rabea 34 posts 186 karma points
    Nov 26, 2018 @ 10:54
    Rabea
    0

    I am calling the partial view using a controller method as the model for partial view is different in different cases. Below is the code:

    MainView.cshtml:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<MainView>
    //Call for dynamic partial view
     @Html.Partial("Sections", Model)
    

    Sections.cshtml:

    @foreach (var section in Model.Sections)
    {        if (!section.Disabled)
        {            @Html.Action("GetSection", "SectionSurface", new { model = section })   }  }
    

    Controller:

     public class SectionSurfaceController : SurfaceController
        {
            [ChildActionOnly]
            public ActionResult GetSection(ArchetypeFieldsetModel model)
            {  
                SectionResolver resolver = new SectionResolver(model); //create instance of section resolver class
                return PartialView(resolver.ViewPath, resolver.Model); //render view with path from resolver and specific model
            }        }
    

    Resolverhelper:

    public class SectionResolver
        {
            private const string ViewsPathRoot = "~/Views/Partials/Sections/"; //default path to the sections views
            private const string ModelsNamespace = "Namespace.Core.Models.ArchetypeModels"; //default namespace for the sections models
            private string _name;
            private ArchetypeFieldsetModel _model;
            public SectionResolver(ArchetypeFieldsetModel model)
            {
                _model = model;
                var name = model.Alias;
    
                if (string.IsNullOrWhiteSpace(name))
                {
                    throw new ArgumentException("Name of section can`t be empty");
                }
                _name = name.First().ToString().ToUpper() + name.Substring(1);
            }
            public string Name
            {
                get
                {
                    return _name;
                }
            }
            public string ViewPath
            {
                get
                {
                    return ViewsPathRoot + _name + ".cshtml";
                }        }
            public object Model
            {            get
                {
                     Type modelType = Type.GetType(ModelsNamespace + "." + _name, false, true); 
                    if (modelType == null)
                    {                    return null;                }
                    return Activator.CreateInstance(modelType, _model); 
                }        }    }}
    

    PartialView:

        @inherits Umbraco.Web.Mvc.UmbracoViewPage<Namespace.Core.Models.ArchetypeModels.TextWithPicture>
    //Rest of Code for view
    

    Any idea? Thanks

Please Sign in or register to post replies

Write your reply to:

Draft