I have created web form, and now I want this data to be stored in the database and form be emailed to the email. This is not working, because it consistently tells me that there is a Server error in '/' Application. Even though I redirect to Thankyou page, and also I have included code for Thank you page. Please tell me what I am doing wrong.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Nordstromapp.Models;
public SupportController(){
_db = new NordstromEntities();
}
//
// GET: /Form/[HttpGet]
public ActionResult Create()
{
var categoryList = new SelectList(new[] {"How the Site Looks",
"Product Offered", "How the site works", "General Comment"});
ViewBag.CategoryList = categoryList;
var concernList = new SelectList(new[] {"Shoes", "Handbags and accessories", "Beauty and Fragrance", "Sales",
"Designe Collection",
"Kids and Babies' collection", "Home gifts", "Service", "Other"});
ViewBag.ConcernList = concernList;
return View();
}
[HttpPost]
public ActionResult Create(Models.Support support)
{
if (string.IsNullOrWhiteSpace(support.Question))
{
ModelState.AddModelError("Question", "Question is required");
}
if (ModelState.IsValid)
{
// save to the database
return Redirect("Thankyou.cshtml");
}
return Create();
}
}
View:
@model Nordstromapp.Models.Support
@{
var Question = Request["Question"];
var First_name = Request["First_name"];
var Last_name = Request["Last_name"];
var Telephone_number = Request["Telephone_number"];
var Email = Request["Email"];
var errorMessage = "";
var debuggingFlag = false;
try {
// Initialize WebMail helper
WebMail.SmtpServer = "localhost";
WebMail.SmtpPort = 21;
WebMail.UserName = "";
WebMail.Password = "";
WebMail.From = "";
// Send email
WebMail.Send(to: Email,
subject: "Help request from" + First_name,
body: Question
);
}
catch (Exception ex) {
errorMessage = ex.Message;
}}
<!DOCTYPE html> <html> <head> <title>Request for Assistance</title></head><br><body><br><p>Sorry to hear that you are having trouble, Mr/Ms <b>@Last_name</b>.</p>
@if(errorMessage == ""){<p>An email message has been sent to our customer service
department regarding the following problem:</p><p><b>@Question</b></p>}
else{<p><b>The email was <em>not</em> sent.</b></p>
<p>Please check that the code in the ProcessRequest page has correct settings for the SMTP server name, a user name,
a password, and a "from" address. </p>
if(debuggingFlag){
<p>The following error was reported:</p><p><em>@errorMessage</em></p> } } </body>
Right off the bat I see a couple issues with the way you end your action.
You should be doing a return RedirectToAction("Create")
at the end or a simple return View()
so you can display model errors (if you add the helpers to your view or a validationsummary block).
You should not redirect to a .chstml page directly. Instead either return View("ThankYou")
or, better yet, redirect to a Get action that displays that page. (This is the Post, Redirect, Get pattern to avoid issues of re-POSTing data if a user refreshes the page.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.