John S.
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
ContactModel.cs - I have this file stored in Models/ViewModels folder
Contact View
THANK YOU View
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
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();
}
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; }
}
}
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>
THANK YOU View
<!DOCTYPE html>
<html>
<body>
<h2>Thanks!</h2>
<div>Your First Name is: </div>
<div>Your Last Name is: </div>
</body>
</html>
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
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.
It your view use type of your working model
And I completely agree with Shaun Vermaak, use scaffold for this task.
<form method="post" action="">
.....
[HttpPost]
public ActionResult ThankYou(ContactModel contactData)
{
return View(contactData);
// or you can use
//View(YourViewName, contactData);
}
You can return View with parameterIt 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>
And I completely agree with Shaun Vermaak, use scaffold for this task.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you all for helping me.