How do I pass a datetime value as a URI parameter in asp.net mvc?

前端 未结 10 1102
孤城傲影
孤城傲影 2020-12-04 18:58

I need to have an action parameter that has a datetime value? Is there a standard way to do this? I need to have something like:

mysite/Controller/Action/2         


        
10条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-04 19:21

    Typical format of a URI for ASP .NET MVC is Controller/Action/Id where Id is an integer

    I would suggest sending the date value as a parameter rather than as part of the route:

     mysite/Controller/Action?date=21-9-2009 10:20
    

    If it's still giving you problems the date may contain characters that are not allowed in a URI and need to be encoded. Check out:

     encodeURIComponent(yourstring)
    

    It is a method within Javascript.

    On the Server Side:

    public ActionResult ActionName(string date)
    {
         DateTime mydate;
         DateTime.Tryparse(date, out mydate);
    }
    

    FYI, any url parameter can be mapped to an action method parameter as long as the names are the same.

提交回复
热议问题