.net core not routing to POST method

Viewed 11561

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

4 Answers

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

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

Related