2015-04-30 2 views
1

Я пытаюсь использовать Postal в Mvc.5.2.3 и Razor.3.2.3 в Visual Studio Ultimate 2013, у меня есть много форм для создания для этого проекта и хотелось бы использовать Views как i хотел бы отправить контент в базу данных, а также отправить электронное письмо с информацией из формы с помощью Postal. Я также создаю частичные представления для верхнего и нижнего колонтитула, чтобы они всегда были одинаковыми, и только содержимое электронной почты было изменено, какие формы используются. Письмо, которое будет отправлено, будет отправлено в отдел продаж, второе письмо должно быть отправлено лицу, заполнившему форму, в которой выражается благодарность, и показывая информацию, которую они отправили с формой. Я надеюсь в этом есть смысл. У меня была база данных, работающая должным образом, но у меня было так много проблем с тем, что система электронной почты работала, я только что начал работу и просто пытался правильно работать с электронной почтой.MVC 5 Использование POSTAL с моделями

My Model

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 = "[email protected]", 
        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 LS Insurance</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 и находится в папка электронной почты под Vi EWS.

+0

Я также Confused о том, как назвать как действия по электронной почте и сохранить в базе данных. и я видел, как использовать задачи, поэтому будет изучать его использование. –

+0

Также я наклоняю шов, чтобы использовать @ Url.Action в моих представлениях, изменилось ли это в MVC 5.2 –

ответ

2

У вас уже есть объект электронной почты, так что просто позвонить отправить:

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

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