Click here to Skip to main content
15,887,821 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am having problem with using c# list variable in jquery while using jquery datepicker. following is my code in .cshmtl file.

If I use "var closedDates = [@String.Join(",", hlist)];" below , this doesn't work. but if I use "var closedDates = ['2017/01/11', '2017/01/12'];, its working fine. So this is a parsing issue I am having. Please help.


@{
   List<string> hlist = new List<string>();
   hlist.Add("2017/01/11");
   hlist.Add("2017/01/12");

 <script type="text/javascript">
$(function () {
                     var year = (new Date).getFullYear();

                     $("#datepicker").datepicker({
                         beforeShowDay: nonWorkingDates,
                         minDate: '+14D',
                         maxDate: new Date(2017, 8, 30)
});
 function nonWorkingDates(date) {
                   var day = date.getDay(), Sunday = 0, Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5, Saturday = 6;
                   @*var closedDates = [@String.Join(",",hlist)];*@
                   var closedDays = [[Sunday]];
                   for (var i = 0; i < closedDays.length; i++) {
                       if (day == closedDays[i][0]) {
                           return [true];
                       }

                   }

                     var closedDates = [@String.Join(",", hlist)];
             
         //var closedDates = ['2017/01/11', '2017/01/12'];
                       var m = date.getMonth(), d = date.getDate(), y = date.getFullYear(), mon = "", day = "";
                       for (i = 0; i < closedDates.length; i++) {
                           m = m + 1;
                           mon = m.toString();
                           if (mon.length < 2) {
                               m = "0" + m;
                           }
                           day = d.toString();
                           if (day.length < 2) {
                               d = "0" + d;
                           }
                           if ($.inArray(y + '/' + m + '/' + d, closedDates) > -1) {
                               return [true];
                           }
                       }
                     
                   

                  

                   return [false];
               }

           });
 </script>
                  

}


What I have tried:

I have also tried parsing like
var closedDates = '<%= this.javaSerial.Serialize(hlist) %>';
but it didn't work as well.

Also
var closedDates = <%= this.javaSerial.Serialize(hlist) %>;
didn't work.
Posted
Updated 7-Dec-16 19:41pm
Comments
F-ES Sitecore 7-Dec-16 11:18am    
javascript runs off of the finished html sent to the browser, it doesn't run from your cshtml page. So view the page source and you'll see something like this;

var closedDates = [2017/01/11, 2017/01/12];

Because that is the html you are generating, at no point are you asking your code to wrap the individual strings in apostrophes. Amend your code to generate the list with the apostrophes and see if it works; always remember to view the page source so you can see exactly what javascript you're executing.
Dhyanga 7-Dec-16 12:06pm    
How to do that? I am so confused. It would be great if you give an example. I thought
var closedDates = [@String.Join(",", hlist)]; line is adding apostrophes to the list.
Karthik_Mahalingam 7-Dec-16 22:34pm    
Always use  Reply  button, to post Comments/query to the user, so that the user gets notified and responds to your text.
Dhyanga 8-Dec-16 11:05am    
oh ok. I will from next time onwards. I have never used comments before so I didn't know that. Well thank you for letting me know.
Karthik_Mahalingam 9-Dec-16 0:28am    
:)

1 solution

var arr = "'"+String.Join("','", hlist)+"'";      


var closedDates = [@Html.Raw(@arr)];


This worked .
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900