Umbraco将模型插入视图

时间:2015-08-17 11:46:01

标签: c# umbraco

我为我的Umbraco网站

创建了一个新视图,模型和控制器

当涉及将模型传递到视图中时,我收到以下错误...

  

传递到字典中的模型项是类型的   ' xxx.Website.Models.FormRenderModel`1 [xxx.Website.Models.NewBrochureSignUpFormModel]&#39 ;,   但是这个字典需要一个类型的模型项   ' xxx.Website.Models.NewBrochureSignUpFormModel'

我的控制器看起来像

    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);
    }

我的视图看起来像

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

我可以看到此视图希望传递NewBrochureSignUpFormModel

然而,当我将NewBrochureSignUpFormModel课程传递给我收到的视图时

  

传递到字典中的模型项的类型为
  &#39; xxx.Website.Models.FormRenderModel`1 [xxx.Website.Models.NewBrochureSignUpFormModel]&#39 ;,   但是这个字典需要一个类型的模型项   &#39; xxx.Website.Models.NewBrochureSignUpFormModel&#39;

EG。转过来!!!

我错过了NewBrochureSignUpFormModel模特课的内容吗?

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; }

 }
}

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

您的问题是您将FormRenderModel传递给View,但View期望xxx.Website.Models.NewBrochureSignUpFormModel。以下是一些快速解决方案:

  1. 您不构建FormRenderModel,只需将原始NewBrochureSignUpFormModel传递给View。我不熟悉CurrentTemplate(...),但我打赌return CurrentTemplate(formModel);会起作用。做这样的事情肯定会有效return View("MyView", formModel);

  2. 您将视图更改为期望FormRenderModel,如:@model FormRenderModel