json - Jquery datepicker - Pass date array from Controller MVC -


i have created list containing available dates , serialized json:

list<string> dates = new list<string>();     foreach(bookingdates itm in model.availabledates) {     dates.add(itm.expdate.tostring("dd-mm-yyyy")); }  model.datesstr = new javascriptserializer().serialize(dates);  

i pass model data jquery , try pass dates datepicker:

var availabledates = @html.raw(json.encode(@model.datesstr)); alert(availabledates);  function available(date) {     dmy = date.getdate() + "-" + (date.getmonth() + 1) + "-" + date.getfullyear();     if ($.inarray(dmy, availabledates) != -1) {         return [true, "ui-available", "available"];     }      else {         return [false, "ui-unavailable", "unavailable"];     } } 

the datepicker not populate dates. if type dates, does:

var availabledates = ["20-11-2016","17-12-2016"]; 

the difference have noticed result of alert when dates typed above is:

20-11-2016,17-12-2016

opposed resulting following when use json:

["20-11-2016","17-12-2016"]

i have tried declaring var array:

var availabledates = []; availabledates = @html.raw(json.encode(@model.datesstr)); 

again no success implementing json. should in way converting json string separated comma?

javascriptserializer().serialize method convert list string. if want javascript array client side code, should change view model property string collection (array/list).

public class yourviewmodel {   public list<string> dates { set;get;}   //other properties  } 

and in action method,

foreach(bookingdates itm in model.availabledates) {     vm.dates.add(itm.expdate.tostring("dd-mm-yyyy")); } return view(vm); 

now in razor view,

@model yourviewmodel <script>    var availabledates = @html.raw(newtonsoft.jsonjsonconvert                                             .serializeobject(model.dates))    console.log(availabledates); </script> 

Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - IE9 error '$'is not defined -