locked
Action method not found error RRS feed

  • Question

  • User585649674 posted

    I have an action method with HTTP post   

    public class BatchReviseController : Controller
    {
    [HttpPost]
    public ActionResult Index(BatchEditRequestModel objBatchReviseRequest)
            { .....
    
    }
    }
    
    ----- CSHTML
    
     @if (User.IsInRole("admin") || User.IsInRole("super"))
                {
                    @Html.Action("Index", "BatchRevise", new { @method="POST"}); 
                }


    It is throwing an error stating Index Method not found in BAtchRevise controller.

    But if i remove new { @method="POST"} from HTML page and also removing [HttpPost] from controller. It is working. I wanted it to be in post mode. Because i wanted to enforce Antiforgery token.

    Please help me, hot to pass antiforgerytoken from inside Html.Action

    Wednesday, June 18, 2014 3:06 AM

Answers

  • User1957004874 posted

    [HttpPost]
    public ActionResult Index(BatchEditRequestModel objBatchReviseRequest)
    

    @if (User.IsInRole("admin") || User.IsInRole("super"))
                {
                    @Html.Action("Index", "BatchRevise", new { @method="POST"}); 
                }

    Here what you are trying to do is a GET operation which will call the action method and render the output in browser window but your action method itself is marked with POST and will not support GET operation.

    In order to support achieve your functionality, you need to have two method one is for GET which will render your view, and then another method with POST (along with ValidateAntiforgeryToken Attribute) to validate the posted data. Also to support post you need to render out the Form tag in your html with submit button.

            [HttpGet]
            public ActionResult Index()
            {
                return View(new SomeModel());
            }
    
            [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Index(SomeModel model)
            {
    
                return View(model);
            }
    @using(Html.BeginForm("Index", "Controller", FormMethod.Post, new {id="myForm"}))
    { 
        @Html.AntiForgeryToken()
        @Html.EditorFor(m => m.Name)
        @Html.EditorFor(m => m.Zip)
        <input type="submit" />
    }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 18, 2014 3:22 AM
  • User-308968715 posted

    As per my knoledge we can't invoke POST method using

    @Html.Action

     helper. You can use Ajax instead.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 20, 2014 9:03 AM

All replies

  • User1957004874 posted

    [HttpPost]
    public ActionResult Index(BatchEditRequestModel objBatchReviseRequest)
    

    @if (User.IsInRole("admin") || User.IsInRole("super"))
                {
                    @Html.Action("Index", "BatchRevise", new { @method="POST"}); 
                }

    Here what you are trying to do is a GET operation which will call the action method and render the output in browser window but your action method itself is marked with POST and will not support GET operation.

    In order to support achieve your functionality, you need to have two method one is for GET which will render your view, and then another method with POST (along with ValidateAntiforgeryToken Attribute) to validate the posted data. Also to support post you need to render out the Form tag in your html with submit button.

            [HttpGet]
            public ActionResult Index()
            {
                return View(new SomeModel());
            }
    
            [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Index(SomeModel model)
            {
    
                return View(model);
            }
    @using(Html.BeginForm("Index", "Controller", FormMethod.Post, new {id="myForm"}))
    { 
        @Html.AntiForgeryToken()
        @Html.EditorFor(m => m.Name)
        @Html.EditorFor(m => m.Zip)
        <input type="submit" />
    }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 18, 2014 3:22 AM
  • User-308968715 posted

    As per my knoledge we can't invoke POST method using

    @Html.Action

     helper. You can use Ajax instead.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 20, 2014 9:03 AM