Copied to clipboard

Flag this post as spam?

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


  • David 14 posts 94 karma points
    Apr 10, 2018 @ 15:27
    David
    0

    Umbraco Contact Form

    I am trying to create a contact form in my Umbraco (7.7.1 build) and I am hitting a few issues. I'm not sure if there is a go to package for sending contact messages or not but my main requirement is to send an email from a contact form.

    I tried the code share tutorial on youtube but kept getting this error

    @{ Html.RenderAction("RenderForm", "ContactSurface"); }
    

    I tried excluding the models folder in App_Data but there was no option only to exclude the files and I also tried rebuilding which fails on several namespaces not having a assembly reference ('Models', 'ContactModel', 'EmailAddressAttribute' 'EmailAddress').

    If anyone can help me with either, that would be great.

  • Nigel Wilson 945 posts 2077 karma points
    Apr 10, 2018 @ 19:14
    Nigel Wilson
    0

    Hi David

    So the key parts to getting things up and running are:

    1. A model (cs class file, can be saved anywhere, but typically I'd create a Models directory on the site and save it there)
    2. A controller (called ContactSurfaceController.cs in this case) in the controllers directory. This controller needs a couple of methods, but for the purposes of getting your form displaying, you need a method called "RenderForm"
    3. A partial view - this will be returned from the RenderAction method in the controller - I typically create a subdirectory called Forms under Views/Partials and save the Partial View there.

    So to figure out what is happening are you able to post the code you have and detail where you have the various "bits" saved.

    What is the URL to the YouTube video ?

    In terms of "goto" - Umbraco Forms is obviously a commercial package that you can use. Alternatively, and this is purely a guess (someone else might be able to confirm) one possibility is the starter kits might have a contact form and source code for you to reference as part of your learning experience ?

    Hope the above helps - feel free to post your code and the community will help out.

    Cheers, Nigel

  • David 14 posts 94 karma points
    Apr 10, 2018 @ 20:29
    David
    0

    Thank you for your reply Nigel.

    So the 3 points you mentioned I had done, which I added while the files where open in visual studio as a web project.

    The video I was watching was this: https://www.youtube.com/watch?v=3V0A1AYJbys

    I followed it practically to the letter but changed the project reference to my own - which I believe to be right as i'm wondering if my set up is a cause of the fault.

    This being the sites hosted online and I download the files via ftp and get a connection to the database still and manage to run a local version of the site - but only through web matrix 3.

    What I have been doing is open web matrix and then launch visual studio from there, then as already mentioned I just followed the video.

    Hope that sheds a bit more light on the subject.

  • Lewis Smith 211 posts 620 karma points c-trib
    Apr 11, 2018 @ 07:13
    Lewis Smith
    0

    Hi David,

    One thing I forget is to reference the controller as an Umbraco controller (SurfaceController) If you haven't done this then this could be what is causing you the issue!

    The surface controller needs to be called when declaring the controller. So if you had a controller called ContactSurfaceController which i believe you do you should start the controller as followed:

    public class ContactSurfaceContoller : SurfaceController{
          public ActionResults RenderForm()
          {
                return PartialView("~/Views/Partials/LinkToYourForm.cshtml");
          }
    }
    

    I believe the surface controller call means that the controller is built into where Umbraco looks for its DLL files.

    If this isn't the issue could you share the code you have for the model, view and controller please?

    Thanks, Lewis

  • David 14 posts 94 karma points
    Apr 11, 2018 @ 09:15
    David
    0

    Thank you Lewis for your reply - I did see that in another post and unfortunately it didn't work for me.

    however here is my code:

    ContactModel.cs (located in Models within the main project)

    using System.ComponentModel.DataAnnotations;
    
    namespace project.Models
    {
        public class ContactModel
        {
            [Required]
            [Display(Name = "First Name:")]
            public string FirstName { get; set; }
    
            [Required]
            [Display(Name = "Last Name:")]
            public string LastName { get; set; }
    
            [Required]
            [Display(Name = "Company Name:")]
            public string CompanyName { get; set; }
    
            [Required]
            [EmailAddress]
            [Display(Name = "Email Address:")]
            public string EmailAddress { get; set; }
    
            [Required]
            [Display(Name = "Message:")]
            public string Message { get; set; }
        }
    }
    

    ContactSurfaceController.cs (located in Controllers within the main project)

    using Umbraco.Web.Mvc;
    using System.Web.Mvc;
    using project.Models;
    using System.Net.Mail;
    
    namespace project.Controllers
    {
        public class ContactSurfaceController : SurfaceController
        {
            public const string PARTIAL_VIEW_FOLDER = "~/Views/Partials/Contact/";
    
            public ActionResult RenderForm()
            {
                return PartialView(PARTIAL_VIEW_FOLDER + "_Contact.cshtml");
            }
    
            public ActionResult SubmitForm()
            {
                if (ModelState.IsValid)
                {
                    SendEmail(model);
                    return RedirectToCurrentUmbracoPage();
                }
                return CurrentUmbracoPage();
            }
    
            private void SendEmail(ContactModel model)
            {
                MailMessage message = new MailMessage(model.EmailAddress, "[email protected]");
                message.Subject = string.Format("Enquiry from {0} {1} - {2}", model.FirstName, model.LastName, model.CompanyName, model.EmailAddress);
                message.Body = model.Message;
                SmtpClient client = new SmtpClient("127.0.0.1", 25);
                client.Send(message);
            }
        }
    }
    

    _Contact.cshtml (Located in View/Partials/Contact/)

    @inherits UmbracoViewPage<project.Models.ContactModel>
    
    @using (Html.BeginUmbracoForm("SubmitForm", "ContactSurface", FormMethod.Post))
    {
        @Html.AntiForgeryToken()
    
        <div class="form-group">
            @Html.ValidationSummary()
        </div>
    
        <div class="form-group">
            <div class="col-xs-3">
                @Html.LabelFor(m => m.FirstName)
            </div>
            <div class="col-xs-9">
                @Html.TextBoxFor(m => m.FirstName)
            </div>
        </div>
    
        <div class="form-group">
            <div class="col-xs-3">
                @Html.LabelFor(m => m.LastName)
            </div>
            <div class="col-xs-9">
                @Html.TextBoxFor(m => m.LastName)
            </div>
        </div>
    
        <div class="form-group">
            <div class="col-xs-3">
                @Html.LabelFor(m => m.CompanyName)
            </div>
            <div class="col-xs-9">
                @Html.TextBoxFor(m => m.CompanyName)
            </div>
        </div>
    
        <div class="form-group">
            <div class="col-xs-3">
                @Html.LabelFor(m => m.EmailAddress)
            </div>
            <div class="col-xs-9">
                @Html.TextBoxFor(m => m.EmailAddress)
            </div>
        </div>
    
        <div class="form-group">
            <div class="col-xs-3">
                @Html.LabelFor(m => m.Message)
            </div>
            <div class="col-xs-9">
                @Html.TextAreaFor(m => m.Message)
            </div>
        </div>
        <button>Submit</button>
    }
    

    and finally the code I am using on the page which I wish to call the form has these elements:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
       @{ Html.RenderAction("RenderForm", "ContactSurface"); }
    

    Hope that helps.

  • David 14 posts 94 karma points
    Apr 12, 2018 @ 07:35
    David
    0

    Anyone able to help with this - I cant seem to see whats going wrong?

  • Nigel Wilson 945 posts 2077 karma points
    Apr 12, 2018 @ 07:54
    Nigel Wilson
    0

    Hi David

    Just wondering and hopefully a silly question, but you are compiling your code via Visual Studio and there is a dll file in the bin directory for your project code ? I ask, as you mention Web Matrix, something I have no idea about, and so wondered if it doesn't compile code... Random thought, but trying to help :-)

    Nigel

  • David 14 posts 94 karma points
    Apr 12, 2018 @ 08:05
    David
    0

    HI Nigel - thank you for your reply.

    I just looked - there are dll files in the bin directory and I have been compiling within visual studio.

  • Nigel Wilson 945 posts 2077 karma points
    Apr 12, 2018 @ 08:12
    Nigel Wilson
    0

    David - think the only thing left is to spin up a test site, drop in your code and see what happens - might be able to work it out.

    I am trying to finish another project so could look into it tomorrow if that is OK...

    The other thought is, are you able to debug the site, ie try going to the page and placing a breakpoint in your controller - does the break point get hit ?

    Cheers, Nigel

  • David 14 posts 94 karma points
    Apr 12, 2018 @ 10:12
    David
    0

    I just realised I haven't posted the Error on this thread this is the error I am receiving.

    No route in the route table matches the supplied values.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.InvalidOperationException: No route in the route table matches the supplied values.

    Source Error:

    Line 25:                         </div>
    Line 26:                         
    Line 27:                         @{ Html.RenderAction("RenderForm", "ContactSurface"); }
    Line 28:                         
    Line 29:      
    

    This is what I get on my local, I will try placing a breakpoint and report back what happens

  • David 14 posts 94 karma points
    Apr 12, 2018 @ 10:53
    David
    0

    Ok, I have gotten a bit futher with this. So after a bit more googling I read about adding the files to the AppCode folder, so I copied out the model and controller file and pasted them in the AppCode folder and tryed again, my local build failed in Visual Studio - but when running it in WebMatrix the error message I got was different this time refering to model, error message below:

    Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

    Compiler Error Message: CS0103: The name 'model' does not exist in the current context

    Source Error:

    Line 19:             if (ModelState.IsValid)
        Line 20:             {
        Line 21:                 SendEmail(model);
        Line 22:                 return RedirectToCurrentUmbracoPage();
        Line 23:             }
    
  • Nigel Wilson 945 posts 2077 karma points
    Apr 13, 2018 @ 00:55
    Nigel Wilson
    0

    Hey David

    You aren't passing in the model on the Submit Method, ie

     public ActionResult SubmitForm(ContactModel model)  
    

    Hope this helps progress things...

    Cheers

    Nigel

Please Sign in or register to post replies

Write your reply to:

Draft