首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MVC 5使用带有模型的POSTAL

MVC 5使用带有模型的POSTAL
EN

Stack Overflow用户
提问于 2015-04-30 16:11:06
回答 1查看 592关注 0票数 1

我正在尝试使用Mvc.5.2.3和Razor.3.2.3在Visual的终极2013,我有许多形式为这个项目,我想使用视图,因为我想把内容张贴到一个数据库,以及发送电子邮件的信息,从表单使用邮政。我也在为页眉和页脚创建部分视图,所以它们总是相同的,只有电子邮件的内容被使用的形式所改变。将发送的电子邮件将被发送到销售部进行审查,第二封电子邮件需要发送给填写该表格的人,向他表示感谢并显示他们发送的信息。我希望这是合理的。我的数据库正常工作,但让电子邮件系统工作有这么多困难,我刚刚开始工作,只是试图使电子邮件系统正常工作。

我的模型

代码语言:javascript
复制
    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;
              }
          }

      }

  }

控制者:

代码语言:javascript
复制
    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);
    }
}

}

表格视图:

代码语言:javascript
复制
    @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")
    }

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-30 16:46:54

您已经有了一个电子邮件对象,所以只需调用send:

代码语言:javascript
复制
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Send(ContactEmail form)
{
    form.Send();

    // You could also save this to the database here...
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29972730

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档