Copied to clipboard

Flag this post as spam?

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


  • John Churchley 27 posts 172 karma points
    Jun 05, 2013 @ 14:46
    John Churchley
    0

    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>
    
    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; } } }
  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Jun 05, 2013 @ 15:04
    Dave Woestenborghs
    0

    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

  • John Churchley 27 posts 172 karma points
    Jun 05, 2013 @ 15:06
    John Churchley
    0

    I tried to delete the post after finding the docs myself not sure why it wouldn't delete.

    Thanks anyhow!

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Jun 05, 2013 @ 15:08
    Dave Woestenborghs
    0

    You are welcome..Also I don't think it's possible to delete posts

  • John Churchley 27 posts 172 karma points
    Jun 05, 2013 @ 16:31
    John Churchley
    0

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

     

     

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Jun 06, 2013 @ 09:27
    Dave Woestenborghs
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft