Copied to clipboard

Flag this post as spam?

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


  • Dan Roma 18 posts 38 karma points
    Nov 18, 2014 @ 05:52
    Dan Roma
    0

    Creating Contact Form in Umbraco 7

    Hi yall - 

    I have a project im working on where I need to create a contact form on each page as a part of the footer..What is the best way to approach this? I have broking my footer into a macro view along with a controller and model. But, for some reason it is not submitting the form. I am using VS 2013 and MVC 4. So, im not sure if I am not doing something incorrectly or not. I am fairly new to mvc. Any feedback would be much appreciated.

    Thanks,

    Dan

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Nov 18, 2014 @ 07:35
    Jan Skovgaard
    0

    Hi Dan

    I think you can get some inspiration from these blogposts http://mytechworld.azurewebsites.net/2014/06/quick-and-easy-umbraco-7-razor-contact-form/ and http://carlosmartinezt.com/2014/06/umbraco-7-and-mvc-contact-form/

    Hope this helps :)

    Oh, and by the way please don't do double posts - It's just going to lead to confusion etc. :)

    /Jan

  • Dan Roma 18 posts 38 karma points
    Nov 18, 2014 @ 16:30
    Dan Roma
    0

    Jan - 

    Thanks for the reply. I get an error loading the Macro on both articles:

    2014-11-18 10:24:20,342 [168] WARN  umbraco.macro - [Thread 130] Error loading Partial View (file: ~/Views/MacroPartials/Footer.cshtml). Exception: System.InvalidOperationException: The model item passed into the dictionary is of type 'Umbraco.Web.Models.PartialViewMacroModel', but this dictionary requires a model item of type 'GCFC.Models.ContactModel'.

       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 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)

    2014-11-18 10:24:21,230 [168] INFO  Umbraco.Web.Mvc.UmbracoPageResult - [Thread 130] Finished (took 2370ms)

    I had something very similiar to these so I was not sure if I am missing something.
  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Nov 18, 2014 @ 17:17
    Jan Skovgaard
    0

    Hi Dan

    Ok, so what does your code in the controller and the footer.cshtml look like? Could you please share? Sounds like you're missing a dictionary item somewhere but not sure.

    /Jan

  • Dan Roma 18 posts 38 karma points
    Nov 18, 2014 @ 17:26
    Dan Roma
    0

    public class ContactSurfaceController : Umbraco.Web.Mvc.SurfaceController

        {

            [ChildActionOnly]

            public ActionResult ContactForm()

            {

                var model = new ContactModel();

     

                //Initialize model however you want

                model.Name = "Enter your name";

     

                //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(ContactModel model)

            {

                // server validation here

                // TempData["ErrorMessage"] = "Error processing field ...";

     

                if (ModelState.IsValid)

                {

                    var currentNode = Umbraco.TypedContent(UmbracoContext.PageId.GetValueOrDefault());

     

                    var homeNode = currentNode.AncestorsOrSelf(0).First();

     

                    var sendEmailsFrom = homeNode.GetPropertyValue<string>("sendEmailsFrom") ?? "[email protected]";

                    var sendEmailsTo = homeNode.GetPropertyValue<string>("sendEmailsTo") ?? "[email protected]";

     

                    var body = String.Format("From: {0}, Email: {1}, Tel: {2} and Message: {3}", model.Name, model.Email, model.Phone, model.Message);

                    var subject = "Travel Concierge - 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...";

     

                        // Clear all the form fields

                        ModelState.Clear();

                        model.Name = string.Empty;

                        model.Phone = string.Empty;

                        model.Email = string.Empty;

                        model.Message = string.Empty;

     

     

                        //redirect to current page to clear the form

                        return RedirectToCurrentUmbracoPage();

                    }

                    catch (Exception ex)

                    {

                        TempData["ErrorMessage"] = ex.Message + ex.StackTrace;

                    }

                }            

    return CurrentUmbracoPage();

    }

        }

     

    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<GCFC.Models.ContactModel>

     

     

    @using (Html.BeginUmbracoForm<GCFC.Controllers.ContactSurfaceController>("ContactForm", new { RedirectPage = 1064 }))

    {

    <div class="row">

        <div class="small-10">

            <div class="row">

                <div class="small-3 columns">

                    <label for="Name" class="right inline">Name</label>

                </div>

                <div class="small-9 columns">

                    <input type="text" placeholder="Your name" id="Name" name="Name" />

                </div>

            </div>

            <div class="row">

                <div class="small-3 columns">

                    <label for="Email" class="right inline">Email</label>

                </div>

                <div class="small-9 columns">

                    <input type="email" placeholder="Your email address" id="Email" name="Email" />

                </div>

            </div>

            <div class="row">

                <div class="small-3 columns">

                    <label for="Subject" class="right inline">Subject</label>

                </div>

                <div class="small-9 columns">

                    <input type="text" placeholder="Regarding..." id="Subject" name="Subject" />

                </div>

            </div>

            <div class="row">

                <div class="small-3 columns">

                    <label for="Message" class="right inline">Message</label>

                </div>

                <div class="small-9 columns">

                    <textarea id="Message" name="Message"></textarea>

                </div>

            </div>

        </div>

        <div class="small-10">

            <div class="row">

                <div class="small-9 small-offset-3 columns">

                    <input type="submit" class="button tiny" value="Send" />

                </div>

            </div>

        </div>

    </div>

    }

  • Biagio Paruolo 1618 posts 1910 karma points c-trib
    Dec 17, 2015 @ 11:08
Please Sign in or register to post replies

Write your reply to:

Draft