Search code examples
.netumbracoumbraco7

Issue with Umbraco SurfaceController Model


I am getting the following error

System.InvalidOperationException: 'The model item passed into the dictionary is of type 'Umbraco.Web.Models.RenderModel', but this dictionary requires a model item of type 'NewSite.Models.ContactModel2'.'

My Template is as follows

@using NewSite.Models
@{
    Layout = "Master.cshtml";
    Html.RenderPartial("~/Views/Contact/Contact.cshtml");
}

My Controller is as Follows

using NewSite.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Umbraco.Web.Models;
using Umbraco.Web.Mvc;

namespace NewSite.Controllers
{
    public class ContactController : SurfaceController
    {
        // GET: Contact
        [HttpGet]
        public ActionResult Index()
        {
            ContactModel2 cmodel = new ContactModel2();
            cmodel.Email = "ddddddaaaaa";
            return PartialView("ContactPartial",cmodel);
        }

        [HttpPost]
        public ActionResult HandleContact(ContactModel2 model)
        {
            if(!ModelState.IsValid)
            {
                return CurrentUmbracoPage();
            }
            ContactModel2 m = new ContactModel2();
            return RedirectToCurrentUmbracoPage();

        }


    }
}

My Partial View is as follows

@model NewSite.Models.ContactModel2

@using (Html.BeginUmbracoForm("HandleContact", "Contact"))
{
    @Html.TextBoxFor(model => model.Email);
    <button name="BtnSubmit" type="submit">Submit</button>
}

My Model is as follows

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

namespace NewSite.Models
{
    public class ContactModel2
    {
        public string Email { get; set; }
    }
}

I have no idea why it thinks I am trying to pass it a RenderModel...


Solution

  • I think you need to pass the appropriate model to the .RenderPartial() Method.

    From the documentation:

    "When Html.RenderPartial() is called with just the name of the partial view, ASP.NET MVC will pass to the partial view the same Model and ViewData dictionary objects used by the calling view template."