0

I have three Action Result methods with same name and all of these are [httpPost] types. i use attribute routing to bound these methods when i add attribute routing non of these method get invoked but when i remove routing attribute from any one of these method, then only that method get invoked. please guide me where i am doing wrong. Thanks in advance.

First method

    [HttpPost, ValidateAntiForgeryToken]
    [Route("Home/PrintFileMovement/{option}/{SearchBox}")]
    public ActionResult PrintFileMovement(string option, string SearchBox)
    {
        FileMovementManagementSystem.FileViewModel.FileViewModel fvm = new FileMovementManagementSystem.FileViewModel.FileViewModel();
        List<File_Movement> fileMovementModel;


        if (option == "DiaryNo")
        {
            //FileMovementManagementSystem.FileViewModel.FileViewModel fvm = new FileMovementManagementSystem.FileViewModel.FileViewModel();
            fileMovementModel = fvm.SearchFileByDiaryNo(SearchBox);
            return View(fileMovementModel);
        }
        else if (option == "Subject")
        {
           // FileMovementManagementSystem.FileViewModel.FileViewModel fvm = new FileMovementManagementSystem.FileViewModel.FileViewModel();
            fileMovementModel = fvm.SearchFileBySubject(SearchBox);
            return View(fileMovementModel);
        }

        fileMovementModel = fvm.GetFileMovement();
        return View(fileMovementModel);
    }

Second method which is working fine without routing attributes

   [HttpPost, ValidateAntiForgeryToken]
   [Route("Home/PrintFileMovement/{option}/{Date:datetime}")]

    public ActionResult PrintFileMovement(string option, DateTime? Date)
    {

        FileMovementManagementSystem.FileViewModel.FileViewModel fvm = new FileMovementManagementSystem.FileViewModel.FileViewModel();
        List<File_Movement> fileMovementModel;
        if (option == "ReceiveDate")
        {

             fileMovementModel = fvm.SearchFileByReceiveDate(Date.Value);
             return View(fileMovementModel);
        }

            fileMovementModel = fvm.GetFileMovement();
            return View(fileMovementModel);
    }

And here is 3rd method

    [HttpPost, ValidateAntiForgeryToken]
    [Route("Home/PrintFileMovement/{MyDate:datetime}")]
    public ActionResult PrintFileMovement(DateTime? MyDate)
    {
        FileMovementManagementSystem.FileViewModel.FileViewModel fvm = new FileMovementManagementSystem.FileViewModel.FileViewModel();
        List<File_Movement> fileMovementModel = fvm.SearchFileByReceiveDate(MyDate.Value);
        return View(fileMovementModel);

    }

RouteConfig

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapMvcAttributeRoutes();

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Login", id = UrlParameter.Optional }
        );



    }
8
  • I have some simular question some time ago. Maybe this will help. Commented Apr 26, 2019 at 4:51
  • It will be helpfull if you add you routing setup (RouteConfig.cs) and controller attributes Commented Apr 26, 2019 at 5:22
  • @vasily.sib please see my updated question code there i added routeconfig.cs code. Commented Apr 26, 2019 at 5:25
  • @vasily.sib please see my updated question code there i added routeconfig.cs code. Commented Apr 26, 2019 at 5:26
  • what about your controller attributes? Do you have some [RoutePrefix("Home")] over public class HomeController : Controller line? Commented Apr 26, 2019 at 5:31

1 Answer 1

0

You need enable routes.MapMvcAttributeRoutes(); in RegisterRoutes

and change DateTime to DateTime? in your parameter and use MyDate.Value in action.

Make sure name of input tag same with parameter name

public ActionResult PrintFileMovement(string option, DateTime? Date)

<input type="text" name="option" />
<input type="text" name="Date" />

Updated:

I have just tried to reproduce your case, because you use POST method, so you need remove /{option}/{Date:datetime} in Route, POST method did not send data via URL.

Change to this will work

[HttpPost, ValidateAntiForgeryToken]
[Route("Home/PrintFileMovement")]

public ActionResult PrintFileMovement(string option, DateTime? Date)

In cshtml file:

<form action="/Home/PrintFileMovement" method="post">
    @Html.AntiForgeryToken();
    @*<input type="text" name="option"/>*@
    <input type="text" name="Date"/>
    <input type="submit" value="Save"/>
</form>
Sign up to request clarification or add additional context in comments.

6 Comments

You mark as HttpPost and you need tool to POST data, or you want use GET method for all? or you can post your View cshtml
My previous comment was not correctly edited, but the question remains. Why do you think they need to change DateTime to DateTime? in action parameters?
@HienNguyen i want to post my view cshtml.
@HienNguyen when i change to this [Route("Home/PrintFileMovement")] then i get resource not found error message
I added cshtml content
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.