i’m using asp.net mvc3 with jquery datepicker widget. i want the user to click on a date and go to a url for that date. eg. http://mysite.com/home/index/20111231
i found a similiar post but it’s not going to a relative url. jQuery UI Datepicker go to date URL
my code works for the first click, but on the second click, the url got double up and gave me 404 error.
1st click: http://mysite.com/home/index/20111231
2nd click: http://mysite.com/home/index/home/index/20111231
code in view:
<script type="text/javascript">
$(document).ready(function () {
$("#datepicker").datepicker({
beforeShowDay: function (date) {
return [true, "active"];
},
onSelect: function (dateText, inst) {
document.location.href = "Home/Index/" + dateText;
},
onChangeMonthYear: function(year, month, inst)
{
},
dateFormat: "yymd",
showOtherMonths: true,
selectOtherMonths: true,
changeMonth: true
});
});
</script>
<h2>@ViewBag.Message</h2>
<div id="datepicker">
</div>
code in controller:
public class HomeController : Controller
{
public ActionResult About()
{
return View();
}
public ActionResult Index(string id)
{
ViewBag.Message = "Welcome " + id;
return View();
}
}
UPDATED:
my solution is to generate the virtual path on the server and stick it to the client html.
document.location.href = '@Request.Url.GetLeftPart(UriPartial.Authority)@HttpRuntime.AppDomainAppVirtualPath' + "/Home/Index/" + dateText;
You need to add a
/to the beginning of your path :document.location.href = "/home/Index/" + dateText;