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

前端 未结 10 999
孤城傲影
孤城傲影 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条回答
  •  有刺的猬
    2020-12-04 19:44

    I thought I'd share what works for me in MVC5 for anyone that comes looking for a similar answer.

    My Controller Signature looks like this:

    public ActionResult Index(DateTime? EventDate, DateTime? EventTime)
    {
    
    }
    

    My ActionLink looks like this in Razor:

    @Url.Action("Index", "Book", new { EventDate = apptTime, EventTime = apptTime})
    

    This gives a URL like this:

    Book?EventDate=01%2F20%2F2016%2014%3A15%3A00&EventTime=01%2F20%2F2016%2014%3A15%3A00
    

    Which encodes the date and time as it should.

提交回复
热议问题