views:

112

answers:

2

I am calling a controller method SendMe(formCollection myForm){}. But I don't know how to pass Form that contains some information. I know how to read it, and also, how to call SendMe() on given controller, but I don't know how to pass it. Though, for the reference I am calling SendMe as:

<div id="menucontainer">

            <ul id="menu">              
                <li><%: Html.ActionLink("Send", "SendMe", "Mail")%></li>
            </ul>

        </div>

and my SendMe method is:

public ActionResult SendMe(FormCollection Form)
    {
        string From = Form["From"].ToString();
        string Pass = Form["Password"].ToString();
        string To = Form["To"].ToString();
        string Subject = Form["Subject"].ToString();
        string Message = Form["Message"].ToString();
        MailMessage nmsg = new MailMessage(From, To, Subject, Message);
        nmsg.IsBodyHtml = true;
        nmsg.Priority = MailPriority.High;
        SmtpClient smtpc = new SmtpClient("smtp.gmail.com", 587);
        smtpc.Credentials = new System.Net.NetworkCredential(From, Pass);
        smtpc.EnableSsl = true;
        smtpc.Send(nmsg);
        return View();
    }

I haven't put anything in Model, though, MailController and SendMe.aspx are in place.

tell me how to get through this. Thanks in advance.

+2  A: 

This simple code should work...if all you need is this to submit information from a form...Please clear if i understood it wrong..I would recommend using a viewmodel or something though to have validation for those fields...and in viewmodel case you should inherit the page from the viewmodel...and retrieve from the viewmodel object instead of formcollection...

<% Html.BeginForm("SendMe", "Send", FormMethod.Post); %> //Here 1st parameter is the actionname while second is the Controller name

    //Form Fields....

<input id="SendMe" type="submit" value="Send Info" />
<% Html.EndForm(); %>
Misnomer
@misnomer - arrg beat me to it. Any case your answer is better than mine
Ahmad
A: 

In order to obtain the FORM variables in your controller, the form needs to be submitted via a Http POST.

<% using (Html.BeginForm()) {%>
  .....
<% Html.EndForm(); %>
Ahmad