具有多个参数(字符串,日期时间)的路由不起作用

时间:2019-04-26 04:38:10

标签: c# asp.net-mvc entity-framework asp.net-mvc-5 routes

我有三个具有相同名称的Action Result方法,所有这些方法都是[httpPost]类型。当我添加属性路由而不是调用这些方法时,我会使用属性路由来绑定这些方法,但是当我从这些方法中的任何一个删除路由属性时,则只会调用该方法。请指导我我做错了什么。预先感谢。

第一种方法

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

第二种方法,无需路由属性即可正常工作

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

这是第三种方法

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



    }

1 个答案:

答案 0 :(得分:0)

您需要在routes.MapMvcAttributeRoutes();中启用RegisterRoutes

并在参数中将DateTime更改为DateTime?,然后使用MyDate.Value

确保输入标签的名称与参数名称相同

public ActionResult PrintFileMovement(string option, DateTime? Date)

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

已更新:

我只是想重现您的情况,因为您使用POST方法,因此需要删除Route中的/{option}/{Date:datetime},POST方法没有通过URL发送数据。

对此进行更改将起作用

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

public ActionResult PrintFileMovement(string option, DateTime? Date)

在cshtml文件中:

<form action="/Home/PrintFileMovement" method="post">
    @Html.AntiForgeryToken();
    @*<input type="text" name="option"/>*@
    <input type="text" name="Date"/>
    <input type="submit" value="Save"/>
</form>