Copied to clipboard

Flag this post as spam?

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


  • Sam 79 posts 426 karma points
    Mar 10, 2016 @ 19:15
    Sam
    0

    Error "dictionary requires a model of type..."

    Hello all, Im new to .net MVC and Im getting an error that Im not sure how to solve.

    I'm trying to create a contact form, and have the controller, model, and a partial view to make it happen, but Im currently 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 'umbraco.Models.ContactModel'.

    error

    here is my Partial:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<umbraco.Models.ContactModel>
    
    <div class="ContactForm">
    @using (Html.BeginUmbracoForm<umbraco.Controllers.ContactSurfaceController>("ContactForm", "ContactSurface"))
    {
        @Html.ValidationSummary(true)
    
    
                <div class="FieldGroup"><label>Name</label><input type='text' name="Name" id="Name" required/></div>    
                <div class="FieldGroup"><label>Email</label><input type='text' name="Email" id="Email" required/></div>
                <div class="FieldGroup"><label>Phone</label><input type='text' name="Phone" id="Phone" /></div>
                <div class="FieldGroup"><label>Company</label><input type='text' name="Company" id="Company" /></div>
                <div class="FieldGroup"><label class="TextAreaLable">Message</label><textarea name="Message" id="Message" required></textarea></div>
                <input type="submit" value="Send Message" class="button" />
    
    }
    </div>
    

    Here is my Model:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Web;
    
    /// <summary>
    /// Summary description for ContactModel
    /// </summary>
    namespace umbraco.Models
    {
    public class ContactModel
    {
        [Required]
        public string Name { get; set; }
        [Required]
        public string Email { get; set; }
        public string Phone { get; set; }
        public string Company { get; set; }
        [Required]
        public string Message { get; set; }
        public int ThankYou { get; set; }
        //
        // TODO: Add constructor logic here
        //
    }
    }
    

    And my Controller:

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Net.Mail;
    using System.Text;
    using System.Web.Http;
    using System.Web.Mvc;
    using umbraco.Models;
    using Umbraco.Forms.Mvc.DynamicObjects;
    using Umbraco.Web.Mvc;
    using Umbraco.Web.UI;
    
    
    namespace umbraco.Controllers
    {
    public class ContactSurfaceController : SurfaceController
    {
        [ChildActionOnly]
        public ActionResult ContactForm()
        { 
            //in case you need it
            var currentNode = Umbraco.TypedContent(UmbracoContext.PageId.GetValueOrDefault());
    
            var model = new ContactModel();
            return PartialView("ContactForm", model);
        }
    
    
        [System.Web.Mvc.HttpPost]
        public ActionResult ContactForm(ContactModel model)
        {
            if (ModelState.IsValid)
            {
                var sb = new StringBuilder();
                sb.AppendFormat("<p>Name: {0}</p>", model.Name);
                sb.AppendFormat("<p>Email: {0}</p>", model.Email);
                sb.AppendFormat("<p>Phone: {0}</p>", model.Phone);
                sb.AppendFormat("<p>Company: {0}</p>", model.Company);
                sb.AppendFormat("<p>{0}</p>", model.Message);
    
                SmtpClient smtp = new SmtpClient();
    
    
                MailMessage message = new MailMessage();
                message.To.Add(new MailAddress(ConfigurationManager.AppSettings["ContactAddress"]));
                message.From = new MailAddress(model.Email);
                message.Body = sb.ToString();
                message.IsBodyHtml = true;
    
                try
                {
                    smtp.Send(message);
                }
                catch (SmtpException ex)
                {    
                //log or manage your error here, then...
                    return RedirectToUmbracoPage(1337); // <-My published error page.
                }
    
                return RedirectToUmbracoPage(1336); // <-My published succes page.
            }
    
            return CurrentUmbracoPage();
        }
    }
    }
    

    The error is pointing out the code that renders the partial view, so I assume it might be an issue with the partial, but I don't know what it is.

    thanks for your time.

  • Mike Chambers 635 posts 1252 karma points c-trib
    Mar 11, 2016 @ 09:47
    Mike Chambers
    101

    @Html.Partial("ContactForm")

    has an override...

    @Html.Partial("ContactForm", new umbraco.Models.ContactModel())

  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    Mar 11, 2016 @ 12:12
    David Brendel
    0

    Hi Sam,

    if you want to call the action in your surface controller to render the Form you have to use HTML.Action("ContactForm", "ContactControllerName") to make a call to the controller.

    HTML.Partial just renders the partial without the trip to the controller. Then the way Mike describes is right.

    Regards David

  • Sam 79 posts 426 karma points
    Mar 11, 2016 @ 16:06
    Sam
    0

    Thank You Mike and David. Both solutions took care of the error. and thank you for the explanation of when to use which.

Please Sign in or register to post replies

Write your reply to:

Draft