Search code examples
c#model-view-controllerumbraco

Partial view gives ambiguous reference error


I'm trying to use a partial view to render the meta data and the header image (separate partial view) into the _layout.cshtml. While doeing this i get the following error: CS0104: 'Meta' is an ambiguous reference between 'Namespace.Models.Meta' and 'Umbraco.Web.PublishedContentModels.Meta'

I tried changing the name of Meta to evreything else, even non meaning keyboard spins like dfguyfgfjhagjhsgf. Still not working.

The meta.cs clase looks as follow:

using System.Collections.Generic;
using System.Linq;
using System.Web;
using Umbraco.Core.Models;
using Umbraco.Web;

namespace Namespace.Models
{
    public class Meta
    {
        public Meta(IPublishedContent content)
        {
            Name = content.Name;

            Description = content.GetPropertyValue<string>("metaDescription");
            Photo = content.GetPropertyValue<IPublishedContent>("metaHeaderImage");
        }

        public IPublishedContent Photo { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }

    }
}

The Meta.cshtml looks as follow:

@model Meta
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title>@Model.Name</title>
<meta name="description" content="@Model.Description">

<meta property="og:title" content="@Model.Name" />
<meta property="og:image" content="@Model.Photo" />

The call for the partial view in the _layout.cshtml is:

@Html.Partial("Meta", Model.Meta)

I would like to know what the error means and a possible solution would be great.

PS. I have done a project in the past with the same setup for partial views as this time. back then it worked just fine. No, i have no longer acces to this older project.


Solution

  • Ambiguous reference error comes when there are two classes with same name in your solution and the code cannot decide which one to use. In your case the class Meta is present in Namespace.Model and also Umbraco.Web.PublishedContentModels. You can change the Meta.cshtml file to refer the correct Meta class by using fully qualified class name like so.

    @model Namespace.Model.Meta