Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing Date Parameter to RedirectToAction RouteValues

When Passing a DateTime Parameter to RedirectToAction (Asp.Net MVC2) either by passing DateTime or by passing a date: "13/4/2000"

    return RedirectToAction("index", "ControllerName",  new { mydate =  DTHelper.PrintDate(myVM.someobject.someobjectDateTime) });

The parameter passed with this representation - which the controller can't resolve:

http://localhost:6105/ControllerName?mydate=19%2F6%2F2011

how can I make it pass as original (it works when I build the url myself):

(this will not work b/c %2F....)

like image 817
Dani Avatar asked Jun 18 '11 19:06

Dani


People also ask

What is difference between RedirectToAction and RedirectToRoute?

RedirectToAction will return a http 302 response to the browser and then browser will make GET request to specified action. Show activity on this post. Ideally I would use RedirectToRoute for Action Links/Images and RedirectToAction in Controller's Action to redirect to another Controller's Action .

How do I redirect to another controller?

Use this: return RedirectToAction("LogIn", "Account", new { area = "" }); This will redirect to the LogIn action in the Account controller in the "global" area.


1 Answers

Try using the following format when passing dates around: yyyy-MM-dd:

var date = myVM.someobject.someobjectDateTime.ToString("yyyy-MM-dd");
return RedirectToAction("index", "ControllerName",  new { mydate = date });

Now inside Index you should be able to get the correct date:

public ActionResult Index(DateTime mydate)
{
    ...
}

and if you wanted a time component use the following format: yyyy-MM-dd HH:mm:ss

like image 68
Darin Dimitrov Avatar answered Oct 05 '22 02:10

Darin Dimitrov