And I have no clue why this happens. I have double and triple checked the template had the correct names and all.
So here it is - my model:
public class ContactFormViewModel
{
[Required]
public string Name { get; set; }
[Required]
[EmailAddress]
public string Email { get; set; }
[Required]
public string Message { get; set; }
}
My controller:
public class ContactFormSurfaceController: Umbraco.Web.Mvc.SurfaceController
{
[ChildActionOnly]
public ActionResult ContactForm()
{
var model = new ContactFormViewModel();
//Initialize model however you want
model.Name = "Enter your full name";
model.Email = "Enter your email address";
model.Message = "Enter your message";
//In case you need to access the current node
var currentNode = Umbraco.TypedContent(UmbracoContext.PageId.GetValueOrDefault());
//In case you need to access the home node
//var home = currentNode.AncestorsOrSelf(0).First();
return PartialView("ContactForm", model);
}
[HttpPost]
public ActionResult ContactForm(ContactFormViewModel model)
{
//server validation
// Do I need this ?
//TempData["Error Message"] = "Error processing field...";
if (ModelState.IsValid)
{
var currentNode = Umbraco.TypedContent(UmbracoContext.PageId.GetValueOrDefault());
var homeNode = currentNode.AncestorOrSelf(0).FirstChild();
var sendEmailsFrom = homeNode.GetPropertyValue<string>("sendEmailsFrom") ?? "[email protected]";
var sendEmailsTo = homeNode.GetPropertyValue<string>("sendEmailsTo") ?? "[email protected]";
var body = string.Format("From: {0}, Email:{1}, Message: {2}", model.Name, model.Email, model.Message);
var subject = "Message Sent";
try
{
umbraco.library.SendMail(sendEmailsFrom, sendEmailsTo, subject, body, true);
TempData["InfoMessage"] =
"Your message has been successfully sent and we will be in touch soon! Thank you for emailing us! ";
//Clear form fields
ModelState.Clear();
model.Name = string.Empty;
model.Email = string.Empty;
model.Message = string.Empty;
//Redirect to the current page, clear of any details
return RedirectToCurrentUmbracoPage();
}
catch (Exception ex)
{
TempData["ErrorMessage"] = ex.Message + ex.StackTrace;
}
}
return CurrentUmbracoPage();
}
Silly me, I had a "RenderAction" instead of "Action" on the view.
However, now I get this error:
System.Web.HttpException was unhandled by user code
HResult=-2147467259
Message=Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'.
Source=System.Web
ErrorCode=-2147467259
WebEventCode=0
StackTrace:
at System.Web.HttpServerUtility.ExecuteInternal(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage, VirtualPath path, VirtualPath filePath, String physPath, Exception error, String queryStringOverride)
at System.Web.HttpServerUtility.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage)
at System.Web.HttpServerUtility.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm)
at System.Web.HttpServerUtilityWrapper.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm)
at System.Web.Mvc.Html.ChildActionExtensions.ActionHelper(HtmlHelper htmlHelper, String actionName, String controllerName, RouteValueDictionary routeValues, TextWriter textWriter)
at System.Web.Mvc.Html.ChildActionExtensions.Action(HtmlHelper htmlHelper, String actionName, String controllerName, RouteValueDictionary routeValues)
at System.Web.Mvc.Html.ChildActionExtensions.Action(HtmlHelper htmlHelper, String actionName, String controllerName)
at ASP._Page_Views_MacroPartials_ContactForm_cshtml.Execute() in d:\workspace\a_nisirius\Nuru\Nurufund\NuruFund2014\Views\MacroPartials\ContactForm.cshtml:line 4
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
at Umbraco.Core.Profiling.ProfilingView.Render(ViewContext viewContext, TextWriter writer)
at Umbraco.Web.Mvc.ControllerExtensions.RenderViewResultAsString(ControllerBase controller, ViewResultBase viewResult)
at Umbraco.Web.Macros.PartialViewMacroEngine.Execute(MacroModel macro, IPublishedContent content)
at Umbraco.Web.Macros.PartialViewMacroEngine.Execute(MacroModel macro, INode node)
at umbraco.macro.LoadPartialViewMacro(MacroModel macro)
at umbraco.macro.renderMacro(Hashtable pageElements, Int32 pageId)
InnerException: System.InvalidOperationException
HResult=-2146233079
Message=The model item passed into the dictionary is of type 'NuruFund2014.Models.ContactFormViewModel', but this dictionary requires a model item of type 'Umbraco.Web.Models.PartialViewMacroModel'.
Source=System.Web.Mvc
StackTrace:
at System.Web.Mvc.ViewDataDictionary`1.SetModel(Object value)
at System.Web.Mvc.ViewDataDictionary..ctor(ViewDataDictionary dictionary)
at System.Web.Mvc.WebViewPage`1.SetViewData(ViewDataDictionary viewData)
at Umbraco.Web.Mvc.UmbracoViewPage`1.SetViewData(ViewDataDictionary viewData)
at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
at Umbraco.Core.Profiling.ProfilingView.Render(ViewContext viewContext, TextWriter writer)
at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1a.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1a.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1a.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass25.<>c__DisplayClass2a.<BeginInvokeAction>b__20()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult)
at System.Web.Mvc.Controller.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
at System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
at System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult)
at System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult)
at System.Web.Mvc.MvcHandler.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)
at System.Web.Mvc.HttpHandlerUtil.ServerExecuteHttpHandlerAsyncWrapper.<>c__DisplayClassa.<EndProcessRequest>b__9()
at System.Web.Mvc.HttpHandlerUtil.ServerExecuteHttpHandlerWrapper.<>c__DisplayClass4.<Wrap>b__3()
at System.Web.Mvc.HttpHandlerUtil.ServerExecuteHttpHandlerWrapper.Wrap[TResult](Func`1 func)
at System.Web.Mvc.HttpHandlerUtil.ServerExecuteHttpHandlerWrapper.Wrap(Action action)
at System.Web.Mvc.HttpHandlerUtil.ServerExecuteHttpHandlerAsyncWrapper.EndProcessRequest(IAsyncResult result)
at System.Web.HttpServerUtility.ExecuteInternal(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage, VirtualPath path, VirtualPath filePath, String physPath, Exception error, String queryStringOverride)
InnerException:
1) One view for a macro, That you are going to use in Richtext editor. and
You call the Controller from this view.
2) Another view for a model CntactFormViewModel", this view generates a actual html input fields.
The error message says about "The model item passed into the dictionary is of type 'NuruFund2014.Models.ContactFormViewModel', but this dictionary requires a model item of type 'Umbraco.Web.Models.PartialViewMacroModel'. "
I think that I can help you. I have made something similar few days ago. First you ned to create the model, for the contact formular, it could look like this:
ContactModel.cs
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace ProjectX.Models
{
public class ContactModel
{
[Required]
public string Name { get; set; }
[Required]
[EmailAddress]
public string Email { get; set; }
[Required]
public string Message { get; set; }
}
}
After you have created the model, then we need to create a controller. My controller contains something like this:
ContactController.cs
using System.Net.Mail;
using System.Web.Mvc;
using Umbraco.ProjectX.Models;
using Umbraco.Web.Mvc;
namespace ProjectX.Controllers
{
public class ContactController : SurfaceController
{
[HttpPost]
public ActionResult Submit(ContactModel model)
{
if (!ModelState.IsValid)
{
return CurrentUmbracoPage();
}
//send email
var message = new MailMessage();
message.To.Add("[email protected]");
message.Subject = "New Contact request";
message.From = new MailAddress(model.Email, model.Name);
message.Body = model.Message;
var smtp = new SmtpClient();
smtp.SetPickupDirectoryLocation();
smtp.Send(message);
TempData["success"] = true;
return RedirectToCurrentUmbracoPage();
}
}
}
The last thing we need to do, is to create a view, that contains our fields to our contact form. When this is done then you need to create a parial view macro where you point to the view so you can to display our created login form on a template. I have made a partial view macro file, that contains this content:
To be able to apply this contact form on a template you need to create a partial view maco file, and point the the created view. The partial view macro file needs to contain this content:
Partial view macro file
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@using ProjectX.Models
@{ Html.RenderPartial("~/Views/Partials/Contact.cshtml", new ContactModel()); }
Remember to make some changes so it suits your needs and your project setup. e.g the project X to the folder where you have your models and controllers.
Umbraco 7 - Contact Form Not Displaying
Hello,
First, I apologize if its the wrong section :( I can't see other posts whenever i go to the forum apart from main categories and I am quite new too!
I've been following these 2 guides: http://carlosmartinezt.com/2014/06/umbraco-7-and-mvc-contact-form/ and http://creativewebspecialist.co.uk/2013/07/22/umbraco-mvc-what-on-earth-is-a-surface-controller/ to learn about surface controllers and create a contact form.
So far, i've managed to trouble shoot all my issues apart from one:
"Error loading Partial View script (file: ~/Views/MacroPartials/ContactForm.cshtml)"
And I have no clue why this happens. I have double and triple checked the template had the correct names and all.
So here it is - my model:
My controller:
and last but not least - my view:
What am I doing wrong? :(
Hi,
Please find
~/Config/umbracoSettings.config
file and change MacroErrors from(inline to throw)
to view the actual errors.Could you share the error message?
I've done as you told me and ran it again, and this is the error I've got:
Silly me, I had a "RenderAction" instead of "Action" on the view.
However, now I get this error:
Could you share the partial view (ContactForm.cshtml)?
The partial view that renders the contact form PartialView("
ContactForm
", model);My ContactForm.cshtml is just the following:
Ok, recreated it and now I have this error going on:
I followed this tutorial step by step :(
http://carlosmartinezt.com/2014/06/umbraco-7-and-mvc-contact-form/
I guess, You should have two views (.cshtml).
1) One view for a macro, That you are going to use in Richtext editor. and You call the Controller from this view.
2) Another view for a model CntactFormViewModel", this view generates a actual html input fields.
The error message says about "The model item passed into the dictionary is of type 'NuruFund2014.Models.ContactFormViewModel', but this dictionary requires a model item of type 'Umbraco.Web.Models.PartialViewMacroModel'. "
This view is for a macro (1)
Another view (2) may look like this one.
Please download it from here and compare your solution. https://www.dropbox.com/s/j7koadwi2khyv0p/umbraco7%20practical%20examples.zip
I cannot download the samples, can anyone share/provide?
https://www.dropbox.com/s/j7koadwi2khyv0p/umbraco7%20practical%20examples.zip
Hi Dave,
I think that I can help you. I have made something similar few days ago. First you ned to create the model, for the contact formular, it could look like this:
ContactModel.cs
After you have created the model, then we need to create a controller. My controller contains something like this:
ContactController.cs
The last thing we need to do, is to create a view, that contains our fields to our contact form. When this is done then you need to create a parial view macro where you point to the view so you can to display our created login form on a template. I have made a partial view macro file, that contains this content:
The partial view contains this:
Contact.cshtml
To be able to apply this contact form on a template you need to create a partial view maco file, and point the the created view. The partial view macro file needs to contain this content:
Partial view macro file
Remember to make some changes so it suits your needs and your project setup. e.g the project X to the folder where you have your models and controllers.
Hope this helps, and make sense,
/Dennis
Dennis
I used your code, works great!
Here is what i changed:
commented the line smtp.SetPickupDirectoryLocation();
in ContactController.cs file
changed "usingUmbraco.ProjectX.Models;" to "usingProjectX.Models;"
and for those who use godaddy email, added:
before smtp.Send(message);
Dave
here is the file, i have manged to save it before it was deleted, i have used the code Dennis posted and thats the one i managed to work for me.
https://drive.google.com/file/d/0B3o4WyDTkLa7djJtTG9lYjhHT3c/view?usp=sharing
Hello,
What's the back office admin user / pass?
Thanks
Sorry i lately saw passwordFormat is clear.
is working on a reply...