简体   繁体   English

如何将DateTime参数从View传递到ASP.net MVC5中的Controller?

[英]How do I pass DateTime parameters from View to Controller in ASP.net MVC5?

I am trying to pass 3 parameters, one int, and 2 datetime to a controller with no luck. 我试图将3个参数,一个int和2个datetime传递给没有运气的控制器。 I have created custom Routes, but no matter what I do it never connects. 我已经创建了自定义路由,但是无论我做什么,它都不会连接。 In My View I have 在我看来,我有

@Html.ActionLink("Check Availability", "Check", new { id = item.RoomID, AD = ArrivalDate.Date.ToString("dd-MM-yyyy"), DD = DepartureDate.Date.ToString("dd-MM-yyyy") }, null)

In my Controller I have 在我的控制器中

   [RoutePrefix("RoomInfoes")]
   [Route("Check/{ID},{AD},{DD}")]
    [HttpPost]
    public ActionResult Check(int? id, string AD, string DD) {

I have tried numerous arrangements of routes and views, but never can connect. 我已经尝试过许多路线和景观的安排,但从来都无法实现。

The code above returns a 404 with 上面的代码返回一个404

Requested URL: /RoomInfoes/Check/1,06-11-2014,06-11-2014

Thanks 谢谢

Do also have the Check Action method for serving HttpGet requests? 还有用于服务HttpGet请求的Check Action方法吗? If not, you will get 404 error. 如果没有,您将得到404错误。

Have you also done the routes.MapRoute in the RouteConfig.cs ? 你也做了routes.MapRouteRouteConfig.cs This is required to render the correct URL with @Html.ActionLink helper method. 使用@Html.ActionLink帮助器方法呈现正确的URL是必需的。

Try adding below code in RouteConfig.cs , if not already exists. 如果尚不存在,请尝试在RouteConfig.cs添加以下代码。

routes.MapRoute(
            name: "RoomInfoes",
            url: "Check/{ID},{AD},{DD}",
            defaults: new
            {
                controller = "RoomInfoes",
                action = "Check",
                ID = UrlParameter.Optional,
                AD = UrlParameter.Optional,
                DD = UrlParameter.Optional
            }
            );

You don't Need Route and the RoutePrefix attribute on the Action method. 您不需要Route和Action方法上的RoutePrefix属性。

Before you use attribute routing make sure you have it enabled: 在使用属性路由之前,请确保已启用它:

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

        routes.MapMvcAttributeRoutes(); // add this line in your route config

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

then decorate your action like this: 然后像这样装饰你的动作:

[Route("RoomInfoes/Check/{ID},{AD},{DD}")]
public ActionResult Test(int? id, string AD, string DD)

The RoutePrefix has been removed because it can be used only on class declaration (the code will even doesn't compile). RoutePrefix已被删除,因为它只能在类声明中使用(该代码甚至不会编译)。 I removed the HttpPost attribute because I assumed that you want make a GET instead of POST. 我删除了HttpPost属性,因为我假设您要进行GET而不是POST。

then to generate a link pointing to this action you can simply write: 然后要生成指向该动作的链接,您可以简单地编写:

@Html.ActionLink("test", "Test", "Home", new { id = 5, AD="58", DD = "58" }, null)

the result will be: 结果将是:

<a href="/RoomInfoes/Check/5%2c58%2c58">test</a> (the commas in your url will be url encoded)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM