[英]pass model property from view to javascript in asp.net MVC5
I'm new to asp.net MVC5, please be patient. 我是asp.net MVC5的新手,请耐心等待。
I have a controller which returns ActionResult with a model,I can use this model through Razor but I'd like to use it in JavaScript. 我有一个控制器,该控制器返回带有模型的ActionResult,我可以通过Razor使用此模型,但我想在JavaScript中使用它。
I searched for examples and tried then reached to this result 我搜索示例,然后尝试达到此结果
$('document').ready(function () { $("#Arrival").datepicker({ minDate: 0, maxDate:userObj , ////here i want to pass model value dateFormat: 'dd/mm/yy', showOn: "button", buttonImage: "img/ui_cal_icon.gif", buttonImageOnly: true, buttonText: "Select date", }); });
this is the script from the view i want to pass this values i got it to maxDate in datepicker 这是我想要传递此值的视图中的脚本,我将其传递给datepicker中的maxDate
<script type="text/javascript"> var userObj = @Html.Raw(Json.Encode(Model.MaximumDaysAheadBookable)); //For javascript object </script>
Thanks for help . 感谢帮助 。
Could just be bad naming, but MaximumDaysAheadBookable
seems like it would be an integer, whereas maxDate
likely requires a JavaScript date or an ISO date string. 可能是不好的命名,但是
MaximumDaysAheadBookable
似乎是整数,而maxDate
可能需要JavaScript日期或ISO日期字符串。 If that's the case, then you would need to do something like: 如果是这种情况,那么您需要执行以下操作:
var maxDate = '@DateTime.Today.AddDays(Model.MaximumDaysAheadBookable).ToString("yyyy-MM-dd")';
It's unnecessary to use Html.Raw
or Json.Encode
for this. 不必为此使用
Html.Raw
或Json.Encode
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.