2015-05-19 44 views
0

我已創建聯繫人我們形成以下代碼:聯繫表格ASP.net MVC

ConatModels.cs

using System; 
using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 
using System.Linq; 
using System.Web; 

namespace DemoVer1.Models 
{ 
    public class ContactModels 
    { 
     [Required(ErrorMessage = "First Name is required")] 

     public string FirstName { get; set; } 
     public string Supject { get; set; } 
     [Required] 

     public string Email { get; set; } 
     [Required] 
     public string Message { get; set; } 

    } 
} 

HomeController.cs:

using DemoVer1.Models; 
    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Net.Mail; 
    using System.Text; 
    using System.Web; 
    using System.Web.Mvc; 

    namespace DemoVer1.Controllers 
    { 
     public class HomeController : Controller 
     { 
      public ActionResult Index() 
      { 
       return View(); 
      } 

      public ActionResult About() 
      { 
       ViewBag.Message = "Your application description page."; 

       return View(); 
      } 

      public ActionResult Contact(ContactModels c) 
      { 
       if (ModelState.IsValid) 
       { 
        try 
        { 
         MailMessage msg = new MailMessage(); 
         SmtpClient smtp = new SmtpClient("smtp.gmail.com"); 
         MailAddress from = new MailAddress(c.Email.ToString()); 
         StringBuilder sb = new StringBuilder(); 
         msg.From = new MailAddress("[email protected]");// replace it with sender email address 

         msg.To.Add("[email protected]");// replace ti with recipient email address 
         msg.Subject = "Contact Us"; 
         smtp.EnableSsl = true; 

         smtp.Credentials = new System.Net.NetworkCredential("[email protected]", " email password"); 
         smtp.Port = 587; 
         sb.Append("First name: " + c.FirstName); 
         sb.Append(Environment.NewLine); 
         sb.Append("Last name: " + c.Supject); 
         sb.Append(Environment.NewLine); 
         sb.Append("Email: " + c.Email); 
         sb.Append(Environment.NewLine); 
         sb.Append("Comments: " + c.Message); 
         msg.Body = sb.ToString(); 
         smtp.Send(msg); 
         msg.Dispose(); 
         return View("Success"); 
        } 
        catch (Exception) 
        { 
         return View("Error"); 
        } 
       } 
       return View(); 
      } 
     } 
    } 

Contact.cshtml

@model DemoVer1.Models.ContactModels 
    @{ 
     ViewBag.Title = "Contact"; 
    } 

    <h1>contact us</h1> 
    @using (Html.BeginForm()) 
    { 

     @Html.ValidationSummary(true) 
     <div class="row"> 
      @Html.LabelFor(model => model.FirstName, "First Name:") 
      @Html.EditorFor(model => model.FirstName) 
      @Html.ValidationMessageFor(model => model.FirstName) 

     </div> 
     <div class="row"> 
      @Html.LabelFor(model => model.Supject, "Last Name:") 
      @Html.EditorFor(model => model.Supject) 
     </div> 
     <div class="row"> 
      @Html.LabelFor(model => model.Email, "Email:") 
      @Html.EditorFor(model => model.Email) 
     </div> 
     <div class="row"> 
      @Html.LabelFor(model => model.Message, "Comments:") 
      @Html.TextAreaFor(model => model.Message) 
     </div> 
     <div class="row"> 
      <input type="submit" value="submit" /> 
      <input type="reset" value="reset" /> 
     </div> 

    } 

如果我運行它,我得到一個錯誤!我不知道是什麼問題。 我感謝您的幫助。感謝提前:)

+5

你是什麼錯誤?它發生在哪裏? –

+0

對我而言出現的錯誤視圖 –

回答

0

因爲形式是你需要寫這樣的動作後形式:

[HttpPost] 
public ActionResult Contact(ContactModels c) 
      { 
//your code 
} 

正如評論您還需要添加[獲取]操作:

[HttpGet] 
public ActionResult Contact() 
{ 
    //your code 
} 
+1

'[Post]'或'[HttpPost]' –

+1

正確,但您還需要提供一個GET操作,該操作返回包含ContactModels類的新實例的視圖。如果你沒有把這個類的新實例傳遞給視圖,你會得到空引用異常。 – hbulens

+0

你說得對,我沒有實例就應用了GET動作,沒有發生任何事情。那麼,你可以通過編碼給我一個例子嗎? –

1

使用:

using System; 
using System.Net; 
using System.Net.Mail; 
using System.Threading.Tasks; 
using System.Web.Mvc; 
using ProjectName.Models; 

控制器:

[HttpPost] 
public async Task<ActionResult> Contact(FormCollection C) 
{ 
     string Name = C["name"]; 
     string Email = C["email"]; 
     string Message = C["message"]; 
     if (ModelState.IsValid) 
     { 
      var body = "<p>Email From: {0} ({1})</p><p>Message:</p><p>{2}</p>"; 
      var message = new MailMessage(); 
      message.To.Add(new MailAddress("[email protected]")); // replace with valid value 
      message.From = new MailAddress("[email protected]"); // replace with valid value 
      message.Subject = "Your email subject"; 
      message.Body = string.Format(body, Name, Email, Message); 
      message.IsBodyHtml = true; 
      using (var smtp = new SmtpClient()) 
      { 
       var credential = new NetworkCredential 
        { 
         UserName = "[email protected]", // replace with valid value 
         Password = "********" // replace with valid value 
        }; 
        smtp.Credentials = credential; 
        smtp.Host = "webmail.Domain.com";//address webmail 
        smtp.Port = 587; 
        smtp.EnableSsl = false; 
        await smtp.SendMailAsync(message); 
        return RedirectToAction("Index"); 
       } 
     } 
       return View(); 
} 

設置web.config中:

<system.net> 
    <mailSettings> 
    <smtp from="[email protected]"> 
     <network host="webmail.Domain.com" 
       port="587" 
       userName="[email protected]" 
       password="password" 
       enableSsl="false" /> 
    </smtp> 
    </mailSettings> 
</system.net> 

觀點:

@using (Html.BeginForm("Contact", "Home", FormMethod.Post, new { @class = "contact-list", id = "contacts_form", role = "form", target = "_blank" })) 
{ 
    @Html.TextBox("name", null, new { @class = "form_item", @required = "required", placeholder = "Name", type = "text" }) 
    @Html.TextBox("email", null, new { @class = "form_item", @required = "required", placeholder = "E-mail", type = "email" }) 
    @Html.TextBox("message", null, new { @class = "form_item", @required = "required", placeholder = "Message", type = "text" }) 
    <input type="submit" value="subscribe" /> 
    }