in Education by
I'm coding a basic portfolio page in ASP.NET Core Razor Pages. I want to have a contact form at the bottom, however I cannot seem to get the Input in the Text fields to my C# class OnPostSendEmail(). I've followed This Tutorial to actually send the email. But I don't have anything to send because I can't pass through my Input to the C# class. By the way, please don't just say "Use Ajax" etc. Here is my .cshtml.cs code: namespace Portfolio.Pages { public class IndexModel : PageModel { public void OnGet() { } public EmailConfig emailConfig = new EmailConfig(); public ActionResult OnPostSendEmail(string fromAddr, string subject, string body, string phone, string firstName, string LastName) { SmtpClient client = new SmtpClient("smtp.gmail.com"); client.UseDefaultCredentials = false; client.Credentials = new System.Net.NetworkCredential("Email", "Password"); MailMessage mailMessage = new MailMessage(); mailMessage.From = new MailAddress(emailConfig.FromAddr); mailMessage.To.Add("ToEmail"); mailMessage.Body = emailConfig.Body + emailConfig.Phone; string tempEmailSub = emailConfig.Subject.ToString(); tempEmailSub += (", ", emailConfig.Name.ToString()); tempEmailSub += (", ", emailConfig.LastName.ToString()); emailConfig.Subject = tempEmailSub; mailMessage.Subject = emailConfig.Subject; client.Send(mailMessage); return null; } } public class EmailConfig { public string FromAddr { get; set; } //public string ToAddr { get; set; } public string Body { get; set; } public string Subject { get; set; } public string Phone { get; set; } public string Name { get; set; } public string LastName { get; set; } } } Here is my HTML form:
And my JavaScript/Ajax $(function () { $("#btn").click(function(e) { e.preventDefault(); var t = '@GetAntiXsrfRequestToken()'; $.ajax({ url: $(this).attr("formaction"), headers: { "RequestVerificationToken": t }, type: "POST", data: { FromAddr: $("#CommentText").val(), Phone: $("#Phone").val(), Subject: $("#Subject").val(), Message: $("#Message").val(), FirstName: $("#FirstName").val(), LastName: $("#LastName").val() }, }).done(function(data) { console.log(data); }).fail(function(a, v, e) { alert(e); }); }); }) JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
For simple way, you could send information from client to server with Razor Model binding. For Razor Model Binding, you need to use [BindProperty]. Here is a working demo: IndexModel public class IndexModel : PageModel { public void OnGet() { } [BindProperty] public EmailConfig emailConfig { get; set; } public ActionResult OnPostSendEmail() { var email = emailConfig; return null; } public class EmailConfig { public string FromAddr { get; set; } //public string ToAddr { get; set; } public string Body { get; set; } public string Subject { get; set; } public string Phone { get; set; } public string Name { get; set; } public string LastName { get; set; } } } For client razor page, there is no need to specify id="Name" type="text" name="name" and you should not append action="" while using asp-page-handler="sendemail"

Related questions

0 votes
    As you know we can set attributes to actionLink or textBox in razor views but how can we set attributes ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 11, 2022 in Education by JackTerrance
0 votes
    As you know we can set attributes to actionLink or textBox in razor views but how can we set attributes ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 11, 2022 in Education by JackTerrance
0 votes
    I'm getting this error: The model item passed into the dictionary is of type System.Data.Entity. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 26, 2022 in Education by JackTerrance
0 votes
    I'm getting this error: The model item passed into the dictionary is of type System.Data.Entity. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 26, 2022 in Education by JackTerrance
0 votes
    This is our setup: Windows 2003 Server Standard, SQL 2005 and ASP.NET 4.0. We have a news site ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I created a sample web application with ASP.NET Core to test the storage and retrieval of the cookie. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    Can ASP.NET Core work with the .NET framework?...
asked Feb 16, 2023 in Technology by JackTerrance
0 votes
    This question already has answers here: Successful Model Editing without a bunch of hidden fields (5 answers ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    I'm new to React with Redux and I've been working on a new web application that has some basic ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 29, 2022 in Education by JackTerrance
0 votes
    I have the following WEB API method, and have a SPA template with Angular: [HttpPost] public IActionResult ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    In Azure, I have a small app deployed: https://jsnamespace.azurewebsites.net/. In localhost, it works ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    In Azure, I have a small app deployed: https://jsnamespace.azurewebsites.net/. In localhost, it works ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 10, 2022 in Education by JackTerrance
0 votes
    Does Server Core 2008 support asp.net? I see references online saying that it isn't supported, but they ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 25, 2022 in Education by JackTerrance
0 votes
    Does Server Core 2008 support asp.net? I see references online saying that it isn't supported, but they ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    Does Server Core 2008 support asp.net? I see references online saying that it isn't supported, but they ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
...