2017-09-29 7 views
0

私は簡単な連絡フォームを作成し、コードを書いてフォームを送信します。受信しますが、空白、件名、本文はありません。ボディやテーマを送信していない連絡先

ここでstackoverlowで同様の問題を確認したところ、私のシナリオでは役に立つと思われる多くのPHPソリューションがありましたが、asp.netは見つかりませんでした。ここ
はコントローラです:

public ActionResult Send(Contact c) 
    { 
     if (ModelState.IsValid) 
     { 
      try 
      { 
       MailAddress sender = new MailAddress(c.Email, c.Name); 

       MailAddress recipient = new MailAddress("[email protected]"); 

       MailMessage Message = new MailMessage(); 
       Message.From = sender; 
       Message.To.Add(recipient); 
       Message.Subject = c.Subject; 
       Message.Body = c.Msg; 
       Message.IsBodyHtml = true; 

       SmtpClient client = new SmtpClient("smtp.gmail.com"); 
       client.EnableSsl = true; 
       client.Port = 587; 
       client.Credentials = new NetworkCredential("[email protected]", "password"); 
       client.Send(Message); 

       return Redirect("/Success.html"); 
      } 
      catch (Exception) 
      { 

       return Redirect("/Error.html"); 
      } 
     } 
     else 
     { 
      return Redirect("www.google.com"); 
     } 
    } 

HTML:

<body> 
<form action="/Mail/Send" method="post"> 
    <div id="glavni"> 
     <p> 
      <label for="TextName">Name:</label> 
      <input id="TextName" type="text" name="Name" required autofocus /> 
     </p> 
     <p> 
      <label for="TextSubject">Subject:</label> 
      <input id="TextSubject" type="text" name="Subject" required /> 
     </p> 
     <p> 
      <label for="TextMail">Email:</label> 
      <input id="TextMail" type="email" name="Email" required /> 
     </p> 
     <p> 
      <label for="TextMsg">Unesite Poruku:</label> 
      <textarea id="TextMsg" type="text" name="Msg" rows="12" cols="20" ></textarea> 
     </p> 
     <p> 
      <button type="submit">Send</button> 
     </p> 
    </div> 

</form> 

MODEL:

public class Contact 
{ 
    public string Name { get; set; } 
    public string Email { get; set; } 
    public string Subject { get; set; } 
    public string Msg {get; set;} 

} 

接触モデルクラスの小道具は、HTML name属性と同じ名前を持ち、何らかの理由で空のエマを送るils。 誰かが光を当てることができますように!

+0

完全な例は示していません。ビューコードも表示する必要があります。 – mason

+0

@mason実践ページ以来、私はMVCを使った単純な空のページに行きました。私はビューを使用せず、単なるHTMLページでした。私はそれを一瞬でリンクします! – MicroDev

+0

また、Contactクラスの定義を提供する必要があります。 – mason

答えて

0

デフォルトコントローラのすべてのアクションはGETメソッドです。したがって、POSTとしてHTTP Webメソッドを指定する必要があります

[HttpPost] 
public ActionResult Send(Contact c) 
+0

しかし、それは私に名前とメールを ' 'から送信しますが、件名やメッセージは送信しません。仕様が問題であれば、まったく送信しないはずのものではありませんか?私は名前とメールを受け取るが、件名とmsgは空である:S – MicroDev

関連する問題