skip to Main Content

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 class="col-sm-12 col-md-9 mtl">
         <button type="submit" id="btnSubmit" class="no-underline btn btn-orange btn-block">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?

2

Answers


  1. 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>
    
    Login or Signup to reply.
  2. fix your action route

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

    and form

    @model UnsubscribeViewModel
    .....
    
     @using (Html.BeginForm("Unsubscribe", "Communication", FormMethod.Post))
    {
    
    ....
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search