I'm trying to use a custom model for a contact form but getting the following error.
The model item passed into the dictionary is of type 'Umbraco.Web.Models.RenderModel', but this dictionary requires a model item of type 'Zanzibar.Web.Models.ContactResponse'.
I suspect I may need to define a custom controller or is there away around this?
@using umbraco.MacroEngines
@using umbraco;
@inherits Umbraco.Web.Mvc.UmbracoViewPage<Zanzibar.Web.Models.ContactResponse>
<div class="ContactForm">
<p>Name: @Html.TextBoxFor(x => x.Name)</p>
<p>Email: @Html.TextBoxFor(x => x.Email)</p>
<p>Telephone Number: @Html.TextBoxFor(x => x.ContactNumber)</p>
<p>
Contact Method: @Html.DropDownListFor(x => x.ContractMethod,
new[] {new SelectListItem() {Text = "Email", Value = "email"},
new SelectListItem() {Text ="Telephone", Value ="telephone"}}, "Choose and option")
</p>
<p>Referral: @Html.DropDownListFor(x => x.Referral, new[]
{ new SelectListItem() { Text = "Internet Search", Value = "InternetSearch" },
new SelectListItem() { Text = "Friend Recommendation", Value = "FriendRecommendation" },
new SelectListItem() { Text = "Professional Recommendation", Value = "ProfessionalRecommendation" },
new SelectListItem() { Text = "Flyer", Value = "Flyer" },
new SelectListItem() { Text = "NetMums", Value = "NetMums" }},
"Choose an option")</p>
<p>Enquiry: @Html.TextAreaFor(x => x.Enquiry)</p>
</div>
Model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Zanzibar.Web.Models {
public class ContactResponse
{
public string Name { get; set; }
public string Email { get; set; }
public string ContactNumber { get; set; }
public string Referral { get; set; }
public string ContractMethod { get; set; }
public string Enquiry { get; set; }
}
}
public class ContactSurfaceController : Umbraco.Web.Mvc.SurfaceController
{
[HttpPost]
public ActionResult ContactForm(ContactFormViewModel model)
{
if (!ModelState.IsValid)
{
return CurrentUmbracoPage();
}
//successful' message on the View, for example:
TempData.Add("CustomMessage", "Your form was successfully submitted at " + DateTime.Now);
return RedirectToCurrentUmbracoPage();
}
}
ViewModel
public class ContactFormViewModel
{
[Required(ErrorMessage = "Please enter you name")]
[Display(Name = "Please enter name here")]
public string Name { get; set; }
public string Email { get; set; }
public string ContactNumber { get; set; }
public string Referral { get; set; }
public string ContractMethod { get; set; }
[Required]
public string Enquiry { get; set; }
}
}
Contact Form in Partial View
I'm trying to use a custom model for a contact form but getting the following error.
The model item passed into the dictionary is of type 'Umbraco.Web.Models.RenderModel', but this dictionary requires a model item of type 'Zanzibar.Web.Models.ContactResponse'.
I suspect I may need to define a custom controller or is there away around this?
View
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage @{ Layout = "Master.cshtml"; } @Html.Partial("ContactForm")Partial View
@using umbraco.MacroEngines @using umbraco; @inherits Umbraco.Web.Mvc.UmbracoViewPage<Zanzibar.Web.Models.ContactResponse> <div class="ContactForm"> <p>Name: @Html.TextBoxFor(x => x.Name)</p> <p>Email: @Html.TextBoxFor(x => x.Email)</p> <p>Telephone Number: @Html.TextBoxFor(x => x.ContactNumber)</p> <p> Contact Method: @Html.DropDownListFor(x => x.ContractMethod, new[] {new SelectListItem() {Text = "Email", Value = "email"}, new SelectListItem() {Text ="Telephone", Value ="telephone"}}, "Choose and option") </p> <p>Referral: @Html.DropDownListFor(x => x.Referral, new[] { new SelectListItem() { Text = "Internet Search", Value = "InternetSearch" }, new SelectListItem() { Text = "Friend Recommendation", Value = "FriendRecommendation" }, new SelectListItem() { Text = "Professional Recommendation", Value = "ProfessionalRecommendation" }, new SelectListItem() { Text = "Flyer", Value = "Flyer" }, new SelectListItem() { Text = "NetMums", Value = "NetMums" }}, "Choose an option")</p> <p>Enquiry: @Html.TextAreaFor(x => x.Enquiry)</p> </div>The best way to use forms in MVC is to use SurfaceControllers.
You can find the docs here : http://our.umbraco.org/documentation/Reference/Mvc/surface-controllers
I tried to delete the post after finding the docs myself not sure why it wouldn't delete.
Thanks anyhow!
You are welcome..Also I don't think it's possible to delete posts
Ok attempted to follow the guidence but still getting the same error update code below:
View
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage @{ Layout = "Master.cshtml"; } @Html.Partial("ContactForm")Partial View
@model Zanzilbar.Web.ViewModels.ContactFormViewModel @using (Html.BeginUmbracoForm("ContactForm", "ContactSurface")) { @Html.EditorFor(x => Model) }Controller
public class ContactSurfaceController : Umbraco.Web.Mvc.SurfaceController { [HttpPost] public ActionResult ContactForm(ContactFormViewModel model) { if (!ModelState.IsValid) { return CurrentUmbracoPage(); } //successful' message on the View, for example: TempData.Add("CustomMessage", "Your form was successfully submitted at " + DateTime.Now); return RedirectToCurrentUmbracoPage(); } }ViewModel
public class ContactFormViewModel { [Required(ErrorMessage = "Please enter you name")] [Display(Name = "Please enter name here")] public string Name { get; set; } public string Email { get; set; } public string ContactNumber { get; set; } public string Referral { get; set; } public string ContractMethod { get; set; } [Required] public string Enquiry { get; set; } } }You are missing a action on your controller to render the Contact Form
[ChildActionOnly] public ActionResult RenderContactForm() { return this.PartialView("ContactForm", new ContactFormViewModel());}
Then from your layout you call this action
@Html.Action("RenderContactForm", "ContactSurface")That should make it work
Dave
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.