Search code examples
c#asp.net-mvc-3viewmodels

Can't seem to get form values (in view) to the controller


I have 2 bits of code here.

This is my controller at the moment:

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);
            }
        }

    }

}

And this is the view that's supposedly associated with it (the part that matters anyways):

<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>

Can someone point me in the right direction? I have no clue what I'm doing wrong.


Solution

  • You don't have xyz_Click() events in your controller. What you have are so-called ActionResults What you need is a) define the target action in your form on your view:

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

    The AntiForgeryToken secures against cross-site-posting by the way.

    Then in your controller create the 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"]);
    

    Aside from that: yes, please learn about MVC basics first. It is quite a bit different to Web Forms. w3schools.com/aspnet/mvc_intro.asp