I have an mvc3 web page that takes a url parameter representing a date, i.e dd-MM-yyyy
public ActionResult Calendar(string startDate, string UserId, string status, bool myPage = false)
I have a partial view within the calendar view which calls an onchange event whenever a ddl is changed, r开发者_开发问答epresenting the status parameter within the method and is also passed into the url.
My map route for the calendar view looks like this:
routes.MapRoute(
"TimesheetCalendar", // Route name
"Timesheet/Calendar/{startDate}/{UserId}", // URL with parameters
new { controller = "Timesheet", action = "Calendar", UserId = UrlParameter.Optional } // Parameter defaults
);
And my javascript onchange event (called within a contained partial view) looks like this:
<script type="text/javascript">
$(function () {
$(".ddl").change(function () { changePage(); });
});
function changePage() {
location.href = '@Url.Action("Calendar", "Controller", new { startDate = @ViewBag.startDate, UserId = (string)null })/' + '@ViewBag.userId' + '?status=' + $('#ControllerStatus').val() + '&myPage=@ViewBag.myPage';
}
In the calender get method in my controller, I set the Viewbag.startdate as date.ToString(dd-MM-yyyy)
.
It works fine, some of the time. And when I change the ddl status the page refreshed the calendar and partial view according to the status. Some of the time however (it seems whenever the week of my calendar is the current week), when I change the ddl, the browser redirects to a 404 error
, and rather than the date as dd-MM-yyyy
in the url, it is showing as dd/MM/yyyy
and thereby completely screwing up the url.
I am new to javascript and have no idea why it would do this? I hope I have given enough info about my code. Anyone know why the string representing date would suddenly have its hyphens - changed to dashes/ ?
Any help greatly appreciated
Sorry. Have just realised that the default ToString
method for dates users a slash/
and that somewhere inbetween passing dates back and forth between the view and its partial view I was setting a date ToString()
and not ToString("dd-MM-yyyy")
精彩评论