2016-05-09 4 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ではなくWebフォームコードです。 MVCにイベントはありません。 MVCサイトに行って基本を学ぶことをお勧めします。 –

+0

誰もがプログラミングのマスターではなく、私は新しく、私は学ぶために最善を尽くしています。 –

+1

はい、@Ruben Pereira、最初に 'asp.net-mvc'の基礎を覗いてみてください。http://www.w3schools.com/aspnet/mvc_intro.asp –

答えて

0

コントローラにxyz_Click()イベントがありません。あなたはあなたが必要なものActionResult S いわゆるいることa)は、あなたのビューにフォーム内のターゲット・アクションを定義 です:

@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フォームとはかなり異なっています。 w3schools.com/aspnet/mvc_intro.asp

+0

ありがとう、私は今私が間違っていたことを考え出しました。 MVCとはまったく異なるものです。 –

関連する問題