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
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
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
Hi Rabea,
How are you calling your partial view? And what does your partial view look like?
Thanks,
Nik
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:
Sections.cshtml:
Controller:
Resolverhelper:
PartialView:
Any idea? Thanks
is working on a reply...