Numan Ahmad
Numan Ahmad

Reputation: 167

Routing with multiple parameters (String, DateTime) is not working

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 }
        );



    }

Upvotes: 0

Views: 349

Answers (1)

Hien Nguyen
Hien Nguyen

Reputation: 18975

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>

Upvotes: 0

Related Questions