简体   繁体   中英

pass model property from view to javascript in asp.net MVC5

I'm new to asp.net MVC5, please be patient.

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.

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

  <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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM