Copied to clipboard

Flag this post as spam?

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


  • Zed 2 posts 22 karma points
    Jun 16, 2015 @ 20:41
    Zed
    0

    Trying to create the SurfaceController contact form example

    Hi all,

    As with many people before me it seems, I'm trying to create the contact form surface controller example, but am having a lot of trouble trying to get it to work. I'm getting the following error:

    *The model item passed into the dictionary is of type 'Umbraco.Web.Models.PartialViewMacroModel', but this dictionary requires a model item of type 'My.Models.LoginFormViewModel'. *

    This is my model (code lifted from the example online):

        namespace My.Models
    {
        public class LoginFormViewModel
        {
            [Required]
            public string UserId { get; set; }
        }
    }
    

    My controller:

    using My.Models;
    using System.Web.Mvc;
    using Umbraco.Web.Mvc;
    
    namespace My.Controllers
    {
        public class LoginFormSurfaceController : SurfaceController
        {
            [ChildActionOnly]
            public ActionResult Index()
            {
                return PartialView("LoginForm", new LoginFormViewModel());
            }
    
            [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult HandleLoginForm(LoginFormViewModel model)
            {
                if (!ModelState.IsValid)
                {
                    return CurrentUmbracoPage();
                }
    
    // do some stuff
    
                TempData["IsSuccessful"] = true;
                return RedirectToCurrentUmbracoPage();
            }
        }
    }
    

    And my form:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<LoginFormViewModel>
    
    @using My.Controllers
    @using My.Models
    
    @{
        Html.EnableClientValidation(true);
        Html.EnableUnobtrusiveJavaScript(true);
    }
    
    @if (Convert.ToBoolean(TempData["IsSuccessful"]))
    {
        <h1>YAY!</h1>
        <p>Thanks for sending your message, we will get back to you shortly.</p>
    }
    else
    {
        using (Html.BeginUmbracoForm<LoginFormSurfaceController>("HandleLoginForm"))
        {
            @Html.ValidationSummary(true)
            @Html.AntiForgeryToken()
    
            @Html.TextBoxFor(model => model.UserId, new { placeholder = "Your ID" })
            @Html.ValidationMessageFor(model => model.UserId)
    
            <input type="submit" value="Send" />
        }
    }
    

    Does anyone have any idea why this isn't working??

    I've been at this for hours and have tried almost everything I've found. One thing I found was that this error can be thrown when a null model is being passed, but I don't know how to check for that, why it is happening or what to do to avoid nulls coming through. (bit of an MVC newbie..)

Please Sign in or register to post replies

Write your reply to:

Draft