似乎无法获得控制器的表单值(在视图中)

时间:2016-05-09 12:35:40

标签: c# asp.net-mvc-3 views models

我这里有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("administrator@aspdotnet-suresh.com");
                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("yourgmailemail@gmail.com", "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>

有人能指出我正确的方向吗?我不知道我做错了什么。

1 个答案:

答案 0 :(得分:0)

您的控制器中没有xyz_Click()个事件。你所拥有的是所谓的ActionResult 你需要的是什么 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 Forms完全不同。 w3schools.com/aspnet/mvc_intro.asp