Model view controller MVC5使用邮政模型

Model view controller MVC5使用邮政模型,model-view-controller,asp.net-mvc-5.2,postal,Model View Controller,Asp.net Mvc 5.2,Postal,我正在尝试在Mvc.5.2.3和Visual studio Ultimate 2013中使用Postal,我有许多表单要为这个项目生成,我希望使用视图,因为我希望将内容发布到数据库,并使用Postal发送一封包含表单信息的电子邮件。我还为页眉和页脚创建了部分视图,因此它们总是相同的,只有电子邮件的内容会因使用的表单而改变。将发送的电子邮件将发送给销售部门进行审核,需要向填写表单的人员发送第二封电子邮件,表示感谢并显示他们随表单发送的信息。我希望这是有道理的。我的数据库工作正常,但有这么多的麻烦让

我正在尝试在Mvc.5.2.3和Visual studio Ultimate 2013中使用Postal,我有许多表单要为这个项目生成,我希望使用视图,因为我希望将内容发布到数据库,并使用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" />&emsp;
                    <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")
    }
@model APP.Models.Forms.Company.ContactEmail
@{
ViewBag.Title=“创建”;
Layout=“~/Views/Shared/\u Layout\u LandingPages.cshtml”;
}
联系方式
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
接触

@Html.ValidationSummary(true,“,new{@class=“text danger”}) @LabelFor(model=>model.FirstName,htmlAttributes:new{@class=“controllabel col-md-4”}) @EditorFor(model=>model.FirstName,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.FirstName,“,new{@class=“text danger”}) @LabelFor(model=>model.LastName,htmlAttributes:new{@class=“controllabel col-md-4”}) @EditorFor(model=>model.LastName,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.LastName,“,new{@class=“text danger”}) @LabelFor(model=>model.BusinessName,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.BusinessName,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.BusinessName,“,new{@class=“text danger”}) @LabelFor(model=>model.Phone,htmlAttributes:new{@class=“controllabel col-md-4”}) @EditorFor(model=>model.Phone,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.Phone,“,new{@class=“text danger”}) @LabelFor(model=>model.UserEmailAddress,htmlAttributes:new{@class=“controllabel col-md-4”}) @EditorFor(model=>model.UserEmailAddress,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.UserEmailAddress,“,new{@class=“text danger”}) @LabelFor(model=>model.Message,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Message,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.Message,“,new{@class=“text danger”})
&emsp; } @ActionLink(“取消”、“索引”、“主页”) @节脚本{ @Scripts.Render(“~/bundles/jqueryval”) }

“我的电子邮件”视图是Contact.cshtml,位于“视图”下的“电子邮件”文件夹中。

您已经有电子邮件对象,因此只需呼叫“发送”:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Send(ContactEmail form)
{
    form.Send();

    // You could also save this to the database here...
}

我还对如何调用操作电子邮件和保存到数据库感到困惑。我看到了如何使用任务,所以会考虑使用它。而且我不能在我的视图中使用@Url.Action,这在MVC 5.2中有没有改变