Link to home
Start Free TrialLog in
Avatar of John S.
John S.Flag for United States of America

asked on

My first C# form - help

I am learning MVC 5. I come from a PHP background, so there is a little learning curve for me.

I want to show a form, do something with the results ( probably email it , but that's not the focus of this question ) - and then on a Thank You page, simply have access to the values of the form submitted.

Here is my skeleton project:

HOME CONTROLLER - partial snippet

        //  Show contact us view
        public ActionResult Contact()
        {
            return View();
        }
       
       // Show Thank You Page
      // DO I NEED [HttpPost] HERE???
        public ActionResult ThankYou(ContactModel contactData)
        {
  
           // HOW DO I PASS contactData to my view?
            return View();
        }

Open in new window


ContactModel.cs - I have this file stored in Models/ViewModels folder


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MyProject.Models.ViewModels
{
    public class ContactModel
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}

Open in new window


Contact View

<!DOCTYPE html>
<html>
<body>

<h2>My First MVC Form</h2>

<form action="???????">
  First name:<br>
  <input type="text" name="firstname" >
  <br>
  Last name:<br>
  <input type="text" name="lastname" ">
  <br><br>
  <input type="submit" value="Submit">
</form> 

</body>
</html>

Open in new window


THANK YOU View

<!DOCTYPE html>
<html>
<body>

<h2>Thanks!</h2>

<div>Your First Name is: </div>
<div>Your Last Name is: </div>
 
</body>
</html>

Open in new window



So that's pretty much where I am.  What do I have to do now to get this thing working?

Also, Where do I put the code to say, email the results? I don't need the code, just where does it go? How do I get access to the form data in my views?

THANKS!! I am excited to see this simple form finally work.

OH, and I know I have to include something like "USING ..." on certain views, but I don't know how.

Regards,

John
Avatar of Shaun Vermaak
Shaun Vermaak
Flag of Australia image

If you add a data source you can easily scaffold all the views
HttpPost method executes once you click on Submit button in MVC form. That means you can process the form data by using HttpPost method.
 <form method="post" action="">
.....
      [HttpPost] 
        public ActionResult ThankYou(ContactModel contactData)
        {
            return View(contactData);
// or you can use   
//View(YourViewName, contactData);
        }

Open in new window

You can return View with parameter

It your view use type of your working model
@using YourNamespace.Models/ViewModels
...
//and you can get properties from your model with..
<div>Your First Name is: @Model.FirstName </div>
<div>Your Last Name is: @Model.LastName  </div>

Open in new window


And I completely  agree with Shaun Vermaak, use scaffold for this task.
ASKER CERTIFIED SOLUTION
Avatar of J0rtIT
J0rtIT
Flag of Venezuela, Bolivarian Republic of image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of John S.

ASKER

Thank you all for helping me.