2016-05-09 18 views
-2

我在這裏有2位代碼。似乎無法獲取表單值(在視圖中)到控制器

這是我此刻的控制器:

using System; 
using System.Net.Mail; 
using System.Web.Mvc; 
using WebMatrix.Data; 

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

      protected void btnSubmit_Click(object sender, EventArgs e) 
     { 
      try 
      { 
       MailMessage Msg = new MailMessage(); 
       // Sender e-mail address. 
       Msg.From = new MailAddress(txtEmail.Text); 
       // Recipient e-mail address. 
       Msg.To.Add("[email protected]"); 
       Msg.Subject = txtSubject.Text; 
       Msg.Body = txtMessage.Text; 
       // your remote SMTP server IP. 
       SmtpClient smtp = new SmtpClient(); 
       smtp.Host = "smtp.gmail.com"; 
       smtp.Port = 587; 
       smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "yourpassword"); 
       smtp.EnableSsl = true; 
       smtp.Send(Msg); 
       //Msg = null; 
       lbltxt.Text = "Thanks for Contact us"; 
       // Clear the textbox valuess 
       txtName.Text = ""; 
       txtSubject.Text = ""; 
       txtMessage.Text = ""; 
       txtEmail.Text = ""; 
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine("{0} Exception caught.", ex); 
      } 
     } 

    } 

} 

這是本應當是與它相關聯(該部分的事項反正)認爲:

<form id="form1" > 
    <div> 
     <table cellspacing="2" cellpadding="2" border="0"> 
      <tr><td></td><td><b>Contact Us Form</b></td></tr> 
      <tr><td><b>Name</b></td><td><asp:TextBox ID="txtName" /></td></tr> 
      <tr><td><b>Email</b></td><td><asp:TextBox ID="txtEmail" /></td></tr> 
      <tr><td><b>Subject</b></td><td><asp:TextBox ID="txtSubject" /></td></tr> 
      <tr><td valign="top"><b>Message</b></td><td> <asp:TextBox ID="txtMessage" Rows="5" Columns="40" TextMode="MultiLine" /></td></tr> 
      <tr><td></td><td><asp:button ID="btnSubmit" Text="Submit" onclick="btnSubmit_Click" CssClass="Button" /></td></tr> 
      <tr><td colspan="2" style=" color:red"><asp:Label ID="lbltxt" /></td></tr> 
     </table> 
    </div> 
</form> 

有人能指出我正確的方向?我不知道我做錯了什麼。

+2

這是網頁表單代碼,而不是asp.net-mvc! MVC中沒有事件。建議您轉到MVC網站並瞭解基礎知識。 –

+0

冷靜下來兄弟,不是每個人都是編程大師,我是新人,我正在盡我所能去學習,如何幫助我而不是小便? –

+1

是的,@Ruben Pereira,請首先通過'asp.net-mvc'基礎知識http://www.w3schools.com/aspnet/mvc_intro.asp –

回答

0

您的控制器中沒有xyz_Click()事件。你有什麼是所謂ActionResult小號 你需要的是一個 )定義了你的觀點在表單中的目標行動:

@using (Html.BeginForm("NameOfYour ActionResult", "NameOfController", FormMethod.Post, new { enctype = "multipart/form-data", @name = "someformname"})) 
    { 
     @Html.AntiForgeryToken() 
//your form code with inputs, combos etc. 

的AntiForgeryToken的方式確保對跨站點張貼。

然後在你的控制器創建的ActionResult:

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult NameOfYourActionResult(FormCollection fcol) 
{ 
//your form evaluation goes here. All your form data is accessible through fcol, e.g. like this: 
//Msg.From = new MailAddress(fcol["txtEmail"]); 

旁白:是的,請先了解MVC基礎。它與Web Forms有很大不同。 w3schools.com/aspnet/mvc_intro.asp

+0

謝謝,我現在已經弄清楚我做錯了什麼,我甚至不知道Web Forms是與MVC完全不同。 –

相關問題