【发布时间】:2011-12-22 05:11:48
【问题描述】:
我正在使用带有 jquery datepicker 小部件的 asp.net mvc3。我希望用户单击日期并转到该日期的网址。例如。 http://mysite.com/home/index/20111231
我找到了一个类似的帖子,但它不会指向相对网址。 jQuery UI Datepicker go to date URL
我的代码在第一次点击时有效,但在第二次点击时,网址翻了一番并给了我 404 错误。 第一次点击:http://mysite.com/home/index/20111231 第二次点击:http://mysite.com/home/index/home/index/20111231
查看代码:
<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>
控制器中的代码:
public class HomeController : Controller
{
public ActionResult About()
{
return View();
}
public ActionResult Index(string id)
{
ViewBag.Message = "Welcome " + id;
return View();
}
}
更新: 我的解决方案是在服务器上生成虚拟路径并将其粘贴到客户端html。
document.location.href = '@Request.Url.GetLeftPart(UriPartial.Authority)@HttpRuntime.AppDomainAppVirtualPath' + "/Home/Index/" + dateText;
【问题讨论】:
标签: jquery asp.net-mvc-3 datepicker relative-path