0

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 .

2
  • 1
    So what exactly is the problem? Commented May 9, 2017 at 18:02
  • @Jasen i want to set the maxdate value in datepicker from model view , you got it ? Commented May 9, 2017 at 21:28

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

So ,this line i will put it in file.js to pass maxdate to datepicker ?? right
MaximumDaysAheadBookable class property has max days i set it after 450 days from today this line set it from today ? how i set it dynamically from Model.MaximumDaysAheadBookable

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.