jmack
jmack

Reputation: 189

Umbraco inserting Models into Views

I have created a new view, model, and controller for a new form for my Umbraco Site

When it comes to passing a model into the view I am receiving the following error…

The model item passed into the dictionary is of type 'xxx.Website.Models.FormRenderModel`1[xxx.Website.Models.NewBrochureSignUpFormModel]', but this dictionary requires a model item of type 'xxx.Website.Models.NewBrochureSignUpFormModel'.

My Controller looks like

    public virtual ActionResult T16_6_NewBrochureRequestForm(RenderModel model, string email, Guid? token)
    {

        var renderModel = new FormRenderModel<NewBrochureSignUpFormModel>(model); << NEW MODEL USING WEIRD <T>class thing
        var formModel = new NewBrochureSignUpFormModel(); << model using new form class


        #region Populate drop downs     <<POPULATING THE MODEL CLASS

        renderModel.FormModel = formModel;
        return CurrentTemplate(renderModel);
    }

My View looks like

@using xxx.Core.Extensions
@using xxx.Logic.Models
@model xxx.Website.Models.NewBrochureSignUpFormModel 

I can see that this view is expecting NewBrochureSignUpFormModel to be passed

However when I pass NewBrochureSignUpFormModel class into the view I receive

The model item passed into the dictionary is of type
'xxx.Website.Models.FormRenderModel`1[xxx.Website.Models.NewBrochureSignUpFormModel]', but this dictionary requires a model item of type 'xxx.Website.Models.NewBrochureSignUpFormModel'.

Eg. Switched around!!!

Am I missing something from my NewBrochureSignUpFormModel model class?

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using xxx.Utils.Extensions.Validation;
using xxx.Core;
using xxx.Logic.Models;

namespace xxx.Website.Models
{
public class NewBrochureSignUpFormModel
{

    public NewBrochureSignUpFormModel()
    {
        Source = FormReferralSource.None;
    }

    public FormReferralSource Source { get; set; }


    [Required(ErrorMessage = "Please select your title")]
    public string SelectedTitle { get; set; }

    [Required(ErrorMessage = "Please add your first name")]
    public string FirstName { get; set; }

    [Required(ErrorMessage = "Please add your last name")]
    public string LastName { get; set; }

    [Required(ErrorMessage = "Please add your date of birth")]
    [RegularExpression(StringValidationPatterns.DateUkPattern, ErrorMessage = "The date is invalid")]
    public string Dob { get; set; }

    [Required(ErrorMessage = "Please add your email")]
    [RegularExpression(StringValidation.EmailPattern, ErrorMessage = "The email is invalid")]
    public string Email { get; set; }

    public bool ContactAboutOffers { get; set; }

    [Required(ErrorMessage = "Please add your telephone number")]
    [RegularExpression(@"^[\d\(\)\+][\d\s\(\)\+\-]{9,}$", ErrorMessage = "Invalid number")]
    public string Phone { get; set; }

    public IEnumerable<SelectListItem> Titles { get; set; }
    public IEnumerable<SelectListItem> BreastProcedures { get; set; }
    public IEnumerable<SelectListItem> FaceProcedures { get; set; }
    public IEnumerable<SelectListItem> NonSurgicalProcedures { get; set; }
    public IEnumerable<SelectListItem> BodyProcedures { get; set; }
    public IEnumerable<SelectListItem> MaleProcedures { get; set; }

 }
}

Any help greatly appreciated.

Upvotes: 0

Views: 175

Answers (1)

bowserm
bowserm

Reputation: 1056

Your problem is that you are passing in a FormRenderModel to the View, but the View is expecting a xxx.Website.Models.NewBrochureSignUpFormModel. Here are a couple of quick solutions:

  1. You don't construct the FormRenderModel and you just pass the raw NewBrochureSignUpFormModel into the View. I'm not familiar with CurrentTemplate(...), but I'll bet return CurrentTemplate(formModel); would work. Doing something like this would definitely work return View("MyView", formModel);

  2. You change your view to expect the FormRenderModel like: @model FormRenderModel

Upvotes: 1

Related Questions