I am creating contact us form using mvc but whenever I click on send message button I get the partial view of error page only the success partial view never called In the loop? kindly help me with it
the code is below.
contact form below code
@inherits UmbracoViewPage
@using ELL.Web.Models
@using (Ajax.BeginForm("SubmitForm", "Contact", new AjaxOptions()
{
UpdateTargetId = "form-result",
HttpMethod = "POST",
InsertionMode = InsertionMode.Replace,
OnSuccess = "contactForm.showResult",
OnFailure = "contactForm.showResult"
}, new { id = "contact-form" }))
{
@Html.TextBoxFor(m => m.Name, new { placeholder = "Name" })
@Html.TextBoxFor(m => m.Email, new { placeholder = "Email" })
using Umbraco.Web.Mvc;
using ELL.Web.Models;
using System.Net.Mail;
using log4net;
using System.Reflection;
namespace ELL.Web.Controllers
{
public class ContactController : SurfaceController
{
public string GetViewPath(string name)
{
return $"/Views/Partials/Contact/{name}.cshtml";
}
[HttpGet]
public ActionResult RenderForm()
{
ContactViewModel model = new ContactViewModel();
return PartialView(GetViewPath("_ContactForm"), model);
}
[HttpPost]
public ActionResult RenderForm(ContactViewModel model)
{
return PartialView(GetViewPath("_ContactForm"), model);
}
[HttpPost]
public ActionResult SubmitForm(ContactViewModel model)
{
bool success = false;
if (ModelState.IsValid)
{
success = SendEmail(model);
}
return PartialView(GetViewPath(success ? "_Success" : "_Error"));
}
public bool SendEmail(ContactViewModel model)
{
ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
try
{
MailMessage message = new MailMessage();
SmtpClient client = new SmtpClient();
string toAddress = System.Web.Configuration.WebConfigurationManager.AppSettings["ContactEmailTo"];
string fromAddress = System.Web.Configuration.WebConfigurationManager.AppSettings["ContactEmailFrom"];
message.Subject = $"Enquiry from: {model.Name} - {model.Email}";
message.Body = model.Message;
message.To.Add(new MailAddress(toAddress, toAddress));
message.From = new MailAddress(fromAddress, fromAddress);
client.Send(message);
return true;
}
catch (System.Exception ex)
{
Log.Error("Contact Form Error", ex);
return false;
}
}
}
}
this is java script for contactform
var contactForm = contactForm || {
init: function () {
this.listeners();
},
listeners: function () {
$(document).on('click', '.contact-submit', function (e) {
e.preventDefault();
var form = $('#contact-form');
form.submit();
})
},
showResult: function () {
$("#form-outer").hide();
$("#form-result").show();
}
};
contactForm.init();
in contact controller the statement return PartialView(GetViewPath(success ? "Success" : "Error")); here the else part is only running it does not return for the success partial view
Hi Ishan
That code looks familiar. Have you tried stepping through the code using debugging?
It would help to know where the code is failing.
My first thought is that it is your SMTP settings in the web.config file.
You can use a tool like SMTP4dev which acts like a mailserver on your local machine and lets you see what the email looks like which would have been sent.
You could instead sign up to something like sendgrid.net and use those smtp settings instead and actually send the email.
I cant get what is wrong in the code it seems like in controller there is some issues and in specifically the if else part when it returns the partial view if result is success or fail, in below line of code :-
thanks sir I got to know the problem, there was problem in the configuration of my smtp, as I work as a trainee I don't have that setup in the system, so the mail was not being send through the smtp.
Thanks sir your video tutorials are great and they are a savior for me as there are not many tutorials on Umbraco , and you are explaining in deep about every single details to lookup on. we are waiting for another tutorials on it to learn more on Umbraco, and be a pro Umbracian!!!!
contact form in website
I am creating contact us form using mvc but whenever I click on send message button I get the partial view of error page only the success partial view never called In the loop? kindly help me with it the code is below. contact form below code @inherits UmbracoViewPage
@using ELL.Web.Models
this is contact controller
using Umbraco.Web.Mvc; using ELL.Web.Models; using System.Net.Mail; using log4net; using System.Reflection;
namespace ELL.Web.Controllers { public class ContactController : SurfaceController {
}
this is java script for contactform
};
contactForm.init();
in contact controller the statement return PartialView(GetViewPath(success ? "Success" : "Error")); here the else part is only running it does not return for the success partial view
Hi Ishan That code looks familiar. Have you tried stepping through the code using debugging?
It would help to know where the code is failing.
My first thought is that it is your SMTP settings in the web.config file. You can use a tool like SMTP4dev which acts like a mailserver on your local machine and lets you see what the email looks like which would have been sent.
You could instead sign up to something like sendgrid.net and use those smtp settings instead and actually send the email.
I hope this helps.
Kind regards
Paul
sir a added that code in web config file which you updated at the end:- this one
and also the one in mail settings also :-
I cant get what is wrong in the code it seems like in controller there is some issues and in specifically the if else part when it returns the partial view if result is success or fail, in below line of code :-
it happens to be like it does not go to the if part and it returns the else part which is error partial view. plz sir guide me what can i do in this.
thanks sir I got to know the problem, there was problem in the configuration of my smtp, as I work as a trainee I don't have that setup in the system, so the mail was not being send through the smtp. Thanks sir your video tutorials are great and they are a savior for me as there are not many tutorials on Umbraco , and you are explaining in deep about every single details to lookup on. we are waiting for another tutorials on it to learn more on Umbraco, and be a pro Umbracian!!!!
is working on a reply...