Reputation: 1859
Link to project here -> https://github.com/crumdev/hqbbq.git
I have a simple 3 page MVC website with 1 controller. For the contact page I have a small 3 input form. Below are the Controller methods:
[Route("contact/")]
public IActionResult Contact()
{
return View();
}
[HttpPost]
public IActionResult Contact(string name, string email, string message)
{
ViewBag.Name = name;
ViewBag.Email = email;
ViewBag.Message = message;
return View();
}
When I submit the form with a breakpoint inside of the Contact method for HttpPost it never breaks but instead uses the regular method and just returns the view again. I have tried reducing my form to just the name field and only capture that and it does not enter the POST method. I have given the regular Contact method the [HttpGet] attribute so it can only be used strictly for GET requests and when I submit the form then it bypasses my controller altogether and returns the Dev exception page that is blank except for "Hello World!" on the screen. I have read through the documentation and followed tutorials on teamtreehouse.com for regular ASP.Net as well but cannot understand why it is behaving this way.
Edit: Here is the code for the page submitting the POST. I am just using a plain HTML form with the POST method for submitting the data.
https://github.com/crumdev/hqbbq/blob/master/HQ-BBQ/Views/Home/contact.cshtml
Upvotes: 8
Views: 13306
Reputation: 3218
I couldn't route to action because i passed object not matched to model, for instance
public class NewsModel : IActive
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long Id { get; set; }
public string NewsName { get; set; }
public string NewsText { get; set; }
public bool Active { get; set; }}
{"id":0,"newsName":"123","newsText":"123","active":""}
active - should be boolean, but i passed string. So first thing one have to do is to remove parameters in Post methods.
[HttpPost] public async Task<IActionResult> PostNews( )
If now you get in action, so you have problem in model, otherwise - you have problem in routing
Upvotes: 0
Reputation: 247018
The POST action needs to have a route as well if the intention is to use attribute routing.
[HttpGet]
[Route("contact")]
public IActionResult Contact() {
return View();
}
[HttpPost]
[Route("contact")]
public IActionResult Contact(string name, string email, string message) {
ViewBag.Name = name;
ViewBag.Email = email;
ViewBag.Message = message;
return View();
}
Note the exclusion of the slashes as they are not needed. Make sure the names and ids of the form inputs match the parameters of the target action
Upvotes: 9
Reputation: 73
You can use the FromForm annotation for your parameters in the controller post method
[HttpPost]
[Route("contact")]
public IActionResult Contact([FromForm]string name, [FromForm]string email, [FromForm]string message)
I would also recommend to use a viewmodel rather than passing all the fields of your form as parameters. Imagine you have a form with 10 fields, your method signature would be a way harder to read
Upvotes: 1
Reputation: 121
It looks like you're missing the Route
attribute on the [HttpPost]
method.
Try this.
[HttpPost]
[Route("contact/")]
public IActionResult Contact(string name, string email, string message)
Also update your view code, so that the name
property of your <input />
tags matches the arguments of your controller action.
Remember that MVC uses the name property to bind to the arguments in your controller action. MSDN Model Binding
For example update your email input to include the name property:
<input name="email" id="email" class="input" type="text" placeholder="Email" value="@ViewBag.Email">
You'll also need to update the text area name to name="message"
.
Upvotes: 2