我想在Visual Studio Ultimate 2013中使用Mvc.5.2.3和Razor.3.2.3中的Postal,我有很多表格要为这个项目生成,并且想要使用Views,因为我想发布内容到数据库以及使用Postal发送包含表单信息的电子邮件。我还为页眉和页脚创建了部分视图,因此它们始终相同,只有电子邮件的内容会被使用的表单更改。将要发送的电子邮件将发送给销售部门进行审核,需要将第二封电子邮件发送给填写表单的人说谢谢并显示他们随表单发送的信息。我希望这是有道理的。我让数据库工作正常但是让电子邮件系统工作起来很麻烦,我刚刚开始尝试让电子邮件系统正常工作。
我的模特
using Postal;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace APP.Models.Forms.Company
{
public class ContactEmail : Email
{
public ContactEmail() : base("Contact")
{ }
public int ContactId { get; set; }
public Guid TicketId { get; set; }
[Required(ErrorMessage = "Please Enter your First Name!")]
[StringLength(100, MinimumLength = 3)]
[DisplayName("First Name")]
[Display(Order = 1)]
public string FirstName { get; set; }
[Required(ErrorMessage = "Please Enter your Last Name!")]
[StringLength(100, MinimumLength = 3)]
[DisplayName("Last Name")]
[Display(Order = 2)]
public string LastName { get; set; }
[DisplayName("Business Name")]
[Display(Order = 3)]
public string BusinessName { get; set; }
[Required(ErrorMessage = "You have not entered a phone numer, Please enter your phone number so we can get back to you!")]
[DataType(DataType.PhoneNumber)]
[DisplayName("Phone Number")]
[RegularExpression(@"^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$", ErrorMessage = "Please enter proper format of one of the following: (555)555-5555, 555-555-5555, 5555555555")]
[StringLength(32)]
[Display(Order = 4)]
public string Phone { get; set; }
[Required(ErrorMessage = "You have not entered an Email address, Please enter your email address!")]
[DataType(DataType.EmailAddress)]
[DisplayName("Email Address")]
[MaxLength(50)]
[RegularExpression(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$", ErrorMessage = "The Email field is not valid, Please enter a valid email address!")]
[Display(Order = 5)]
public string UserEmailAddress { get; set; }
[Required(ErrorMessage = "You have not entered a message, Please enter a message!")]
[DataType(DataType.MultilineText)]
[StringLength(2000)]
[DisplayName("Message")]
[Display(Order = 6)]
public string Message { get; set; }
public Source Source { get; set; }
public HttpPostedFileBase Upload { get; set; }
[Display(Name = "Full Name")]
public string FullName
{
get
{
return LastName + ", " + FirstName;
}
}
}
}
控制器:
using APP.Models;
using APP.Models.Forms.Company;
using Postal;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Web;
using System.Web.Mvc;
namespace APP.Controllers
{
public class FormsController : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
#region Main Forms Page
// Forms Page Blank with unautherized access
public ActionResult Index()
{
return View();
}
#endregion
#region Contact_Form
// GET: Forms/Create
public ActionResult Contact()
{
return View();
}
// POST: Forms/Submit
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Send(ContactEmail form)
{
var email = new Email("Contact")
{
To = "webmaster@somedomain.com",
MyModel = ContactEmail //Says its a "type" but used like a variable.
}
email.Send();
}
#endregion
#region Condo Form
#endregion
#region Personal Flood Form
#endregion
#region Home Insurance Form
#endregion
#region Renters Insurance Form
#endregion
#region WaterCraft Insurance Form
#endregion
#region Life Insurance Form
#endregion
#region Business Flood Form
#endregion
#region Business Risk Form
#endregion
#region Business Inland Marine Form
#endregion
#region Business Group Health Form
#endregion
#region Form
#endregion
#region Not Available Forms Page
// Forms Page Blank with unautherized access
public ActionResult Not_Available()
{
return View();
}
#endregion
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
表单视图:
@model APP.Models.Forms.Company.ContactEmail
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout_LandingPages.cshtml";
}
<div class="box">
<h2>Contact Form</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Contact </h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="row">
<div class="col-lg-6">
<div class="form-group">
@Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-4" })
<div class="col-md-8">
@Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
@Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-4" })
<div class="col-md-8">
@Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="col-lg-12">
<div class="form-group">
@Html.LabelFor(model => model.BusinessName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.BusinessName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.BusinessName, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
@Html.LabelFor(model => model.Phone, htmlAttributes: new { @class = "control-label col-md-4" })
<div class="col-md-8">
@Html.EditorFor(model => model.Phone, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Phone, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
@Html.LabelFor(model => model.UserEmailAddress, htmlAttributes: new { @class = "control-label col-md-4" })
<div class="col-md-8">
@Html.EditorFor(model => model.UserEmailAddress, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.UserEmailAddress, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="col-lg-12">
<div class="form-group">
@Html.LabelFor(model => model.Message, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Message, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Message, "", new { @class = "text-danger" })
</div>
</div>
</div></div>
<hr />
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Submit Form" class="btn btn-default" /> 
<input type="reset" value="Reset Form" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Cancel", "Index", "Home")
</div>
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
我的电子邮件视图是Contact.cshtml,位于“视图”下的电子邮件文件夹中。
答案 0 :(得分:2)
您已有电子邮件对象,因此请致电发送:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Send(ContactEmail form)
{
form.Send();
// You could also save this to the database here...
}