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'.
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.
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.
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'.
here is my Partial:
Here is my Model:
And my Controller:
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.
@Html.Partial("ContactForm")
has an override...
@Html.Partial("ContactForm", new umbraco.Models.ContactModel())
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
Thank You Mike and David. Both solutions took care of the error. and thank you for the explanation of when to use which.
is working on a reply...