Copied to clipboard

Flag this post as spam?

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


  • AnithaUnitedhands 4 posts 44 karma points
    Jan 04, 2023 @ 09:00
    AnithaUnitedhands
    0

    Could not find a Surface controller route in the RouteTable for controller name ContactSurfaceController

    Hi, I am using Umbraco 10. I'm attempting to use Surface Controller to send a mail via the Contact Page. But I am unable to use Umbraco.Web.Mvc, instead I am using Umbraco.Cms.Web.Website.Controllers Namespace. I ran the code and filled out all the fields on the contact page, but nothing happened when I clicked the submit button. I am getting this error "Could not find a Surface controller route in the RouteTable for controller name ContactSurfaceController". Is this because of the different namespace I used or some other issue?? Please Someone assist me. Thanks in Advance.

    Controller:

    ContactSurfaceController.cs:

    public class ContactSurfaceController : SurfaceController{
    
        private readonly IEmailSender _emailSender;
        private readonly ILogger<ContactSurfaceController> _logger;
        private readonly GlobalSettings _globalSettings;
    
        public ContactSurfaceController(
            IUmbracoContextAccessor umbracoContextAccessor, 
            IUmbracoDatabaseFactory databaseFactory, 
            ServiceContext services, 
            AppCaches appCaches, 
            IProfilingLogger profilingLogger, 
            IPublishedUrlProvider publishedUrlProvider, 
            IEmailSender emailSender, 
            ILogger<ContactSurfaceController> logger, 
            IOptions<GlobalSettings> globalSettings)
            : base(umbracoContextAccessor, databaseFactory, 
                  services, appCaches, profilingLogger, 
                  publishedUrlProvider)
        {
            _emailSender = emailSender;
            _logger = logger;
            _globalSettings = globalSettings.Value;
        }
    
        [HttpPost]
        public async Task<IActionResult> SubmitForm(ContactForm model)
        {
            if (!ModelState.IsValid) return CurrentUmbracoPage();
    
            TempData["Success"] = await SendEmail(model);
    
            return RedirectToCurrentUmbracoPage();
        }
    
        public async Task<bool> SendEmail(ContactForm model)
        {
            try
            {
                var toAddress = _globalSettings.Smtp.From;
                var subject = string.Format("Enquiry from: {0} - {1}", model.FullName, model.Email);
    
                EmailMessage message = new EmailMessage(model.Email, toAddress, subject, model.YourQuestions, false);
                await _emailSender.SendAsync(message, emailType: "Contact");
    
                _logger.LogInformation("Contact Form Submitted Successfully");
                return true;
            }
            catch (System.Exception ex)
            {
                _logger.LogError(ex, "Error When Submitting Contact Form");
                return false;
            }
        }
    }
    

    Models:

    ContactForm.cs:

        public class ContactForm{
    
        [Required]
        public string FullName { get; set;}
    
        [Required]
        public string LastName { get; set;}
    
        [Required]
        [DataType(DataType.EmailAddress)]
        public string Email { get; set;}
    
        [Required]
        [DataType(DataType.PhoneNumber)]
        public long PhoneNumber { get; set;}
    
        [Required]
        public string YourQuestions { get; set;}
    
    }
    

    Views:

    Contactus.cshtml:

    @using (Html.BeginUmbracoForm("SubmitForm","ContactSurfaceController", FormMethod.Post)){
     <div class="row input-container line_input">
       <div class="col-md-6 col-sm-12">
         <div class="styled-input">
         <input type="text" id ="FullName" name ="FullName" required  />
               <label> @Model?.FullName<span>*</span> </label>
           </div>
         </div>
      <div class="col-xs-12"></div>
    
    
          <button type="submit" class="submit_btn" value="Submit Your Request">Submit Your Request</button>
       </div>
                  }
    
  • Huw Reddick 1932 posts 6722 karma points MVP 3x c-trib
    Jan 04, 2023 @ 09:26
    Huw Reddick
    100

    You should refer to the controller as ContactSurface not ContactSurfaceController, the Cotroller part is not necesary

  • Huw Reddick 1932 posts 6722 karma points MVP 3x c-trib
    Jan 04, 2023 @ 09:28
    Huw Reddick
    100

    you can use either

    using (Html.BeginUmbracoForm("SubmitForm", "ContactSurface", ...
    

    or

    using (Html.BeginUmbracoForm<ContactSurfaceController>("SubmitForm", ...
    
  • AnithaUnitedhands 4 posts 44 karma points
    Jan 04, 2023 @ 09:35
    AnithaUnitedhands
    0

    Thank you.. Its working now..

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies