2017-11-02 12 views
0

ドロップダウン選択に基づいて特定の電子メールアドレスに電子メールをリダイレクトすることができました。メールを[email protected]に移動するか、ドロップダウンから選択した情報が[email protected]に移動する必要があります。私はすでにフォームを作成していますが、MVC actionResultと@Razorビューを使用してこれを行う方法についてはわかりません。MVCビューのドロップダウン選択に基づいて専用電子メールアドレスに電子メールを送信するにはどうすればいいですか?

私の見解

@model ETWServices.Models.ContactUs 
@{ 
    var culture = System.Threading.Thread.CurrentThread.CurrentUICulture.Name.ToLowerInvariant(); 
} 
<div class="cards"> 
    <div class="card"> 

     <div id="contact-form" class="clearfix"> 
      @Html.ValidationSummary(true) 
      @using (Html.BeginForm()) 
      { 
       <h1> 
        @Resources.ResContact.Title 
       </h1> 

         <div class="editor-label"> 
          @Html.LabelFor(model => model.Name) 
         </div> 
         <div class="editor-field"> 
          @Html.TextBoxFor(model => model.Name) 
          @Html.ValidationMessageFor(model => model.Name) 
         </div> 
         <div class="editor-label"> 
          @Html.LabelFor(model => model.Email) 
         </div> 
         <div class="editor-field"> 
          @Html.TextBoxFor(model => model.Email) 
          @Html.ValidationMessageFor(model => model.Email) 
         </div> 
         <div class="editor-label"> 
          @Html.LabelFor(model => model.Subject) 
         </div> 
         <div class="editor-field"> 
          @Html.TextBoxFor(model => model.Subject) 
          @Html.ValidationMessageFor(model => model.Subject) 
         </div> 
         <div class="editor-label"> 
          @Html.LabelFor(model => model.Message) 
         </div> 
         <div class="editor-field"> 
          @Html.TextAreaFor(model => model.Message) 
          @Html.ValidationMessageFor(model => model.Message) 
         </div> 
         <p> 
          <div class="submit"> 
           <input type="submit" value="@Resources.ResContact.Send" id="btnSubmit" /> 
          </div> 
         </p> 
       } 

     </div> 

    </div> 

</div> 

お問い合わせは、コントローラ

[HttpPost] 
      public ActionResult Contact(ContactUs contUs) 
      { 
       if (ModelState.IsValid) 
       { 
        try 
        { 
         MailMessage mailMsg = new MailMessage(); 
         mailMsg.From = new MailAddress(contUs.Email); 
         mailMsg.To.Add("[email protected]"); 
         mailMsg.Subject = contUs.Subject; 
         mailMsg.Body = contUs.Message; 

        SmtpClient smtp = new SmtpClient(); 

        smtp.Host = "smtp.gmail.com"; 
        smtp.Port = 587; 
        smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "gmailpassword"); 
        smtp.EnableSsl = true; 
        smtp.Send(mailMsg); 
        ModelState.Clear(); 

       } 
       catch (Exception ex) 
       { 
        ModelState.Clear(); 
        ViewBag.Message = " Sorry we are facing Problem here "; 
       } 
      } 
      return View(); 
     } 

お問い合わせモデル

public class ContactUs 
    { 
     [Display(Name = nameof(Resources.ResContact.Name), ResourceType = typeof(Resources.ResContact))] 
     [Required(ErrorMessageResourceType =typeof(Resources.ResContact), 
      ErrorMessageResourceName = nameof(Resources.ResContact.NameReq))] 
     [StringLength(20, MinimumLength = 5,ErrorMessageResourceType =typeof(Resources.ResContact), 
      ErrorMessageResourceName = "NameShort")] 
     public string Name { get; set; } 

     [Display(Name = "Email", ResourceType =typeof(Resources.ResContact))] 
     [Required(ErrorMessageResourceType = typeof(Resources.ResContact), 
      ErrorMessageResourceName = "Emailrequired")] 
     [RegularExpression("[email protected]+\\..+", ErrorMessageResourceType = typeof(Resources.ResContact), 
            ErrorMessageResourceName = "EmailWrong")] 
     public string Email { get; set; } 

     [Required(ErrorMessageResourceType =typeof(Resources.ResContact), 
      ErrorMessageResourceName ="subjectRequired")] 
     [Display(Name = "Subject", ResourceType = typeof(Resources.ResContact))] 
     public string Subject { get; set; } 

     [Required(ErrorMessageResourceType = typeof(Resources.ResContact), 
      ErrorMessageResourceName = "msgRequired")] 
     [Display(Name = "Message", ResourceType = typeof(Resources.ResContact))] 
     public string Message { get; set; } 

    } 

答えて

0

シンプルなソリューション、フォームにSELECT要素を追加するSELECT要素のname属性の値と一致しているあなたのビューモデルに新しいプロパティを追加しています。

<SELECT name="toAddress"> 
    <option value="info">Info</option> 
    <option value="general">General</option> 
</SELECT> 

今、ユーザーがフォームを送信する際さて、プロパティToAddressあなたのメソッドのパラメータは、選択した値(infoまたはgeneral)を持つことになります

public class ContactUs 
{ 
    public string ToAddress { set;get;} 
    // your existing properties goes here 
} 

同じ名前を使用してビューモデルにプロパティを1つ追加し、。それをさらに使用して完全な電子メールアドレスを構築することができます。

[HttpPost] 
public ActionResult Contact(ContactUs contUs) 
{ 
    var toSelection = contUs.ToAddress; 
    //use this as needed 
} 

ここでは、2つのオプションでSELECT要素をハードコードしました。しかし、DropDownListForヘルパーメソッドのように、ドロップダウンをレンダリングする他の方法もあります。

関連する問題