Home > Mobile >  ASP.NET MVC - I can't figure out why my POST action is not being hit
ASP.NET MVC - I can't figure out why my POST action is not being hit

Time:04-15

I haven't worked in ASP.NET MVC for a few years so I'm a little rusty. I can't figure out what I'm missing so that my POST Action isn't being hit when I submit my form. Here's my view:

<form id="unsubscribe-form" method="post" action="Communication/Unsubscribe">
     <div >
         <button type="submit" id="btnSubmit" >Yes, I'm Sure</button>
     </div>
</form>

Here's my action:

[Route("Communication/Unsubscribe")]
[AnyAccessType]
[HttpPost]
public ActionResult Unsubscribe(UnsubscribeViewModel model)

Is there something obvious I'm missing as to why this Action wouldn't be hit?

CodePudding user response:

Take a look at this question Here and this post Here

Should look something like this. If in doubt try hitting your controller with postman and see what happens. Hope this helps

@model Form_Post_MVC.Models.PersonModel


@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
</head>
<body>
    @using (Html.BeginForm("Index", "Home", FormMethod.Post))
    {
        <table cellpadding="0" cellspacing="0">
            <tr>
                <th colspan="2" align="center">Person Details</th>
            </tr>
            <tr>
                <td>PersonId: </td>
                <td>
                    @Html.TextBoxFor(m => m.PersonId)
                </td>
            </tr>
            <tr>
                <td>Name: </td>
                <td>
                    @Html.TextBoxFor(m => m.Name)
                </td>
            </tr>
            <tr>
                <td>Gender: </td>
                <td>
                    @Html.DropDownListFor(m => m.Gender, new List<SelectListItem>
                   { new SelectListItem{Text="Male", Value="M"},
                     new SelectListItem{Text="Female", Value="F"}}, "Please select")
                </td>
            </tr>
            <tr>
                <td>City: </td>
                <td>
                    @Html.TextBoxFor(m => m.City)
                </td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" value="Submit"/></td>
            </tr>
        </table>
    }
</body>
</html>

CodePudding user response:

fix your action route

[Route("~/Communication/Unsubscribe")]
public ActionResult Unsubscribe(UnsubscribeViewModel model)

and form

@model UnsubscribeViewModel
.....

 @using (Html.BeginForm("Unsubscribe", "Communication", FormMethod.Post))
{

....
}
  • Related