Accessing C# variable from Javascript in asp.net mvc application

.net, asp.net-mvc, c#, javascript, razor

Solution

Make a foreach razor loop within javascript :

var date = new Date();
var d = date.getDate();
var m = date.getMonth();
var y = date.getFullYear();
$('#calendar').fullCalendar({
    theme: true,
    header: {left: 'prev,next today',center: 'title',right: 'month,agendaWeek,agendaDay'},
    editable: true,
    events: [
    @
    {
      bool isFirst = true;
    }
    @foreach(var m in Model)
    {
        if(!isFirst)
        {
          @:,
        }

        @:{title: @m.Tache_description, ...<other properties here>}

        isFirst = false;
    }
    ]
});

Problem

I have a problem in `How to use javascript variables in C# and vise versa` : I have this `Model` passing to the view: ``` public List<Tache> Get_List_Tache() { Equipe _equipe = new Equipe(); List<Tache> liste_initiale = _equipe.Get_List_tache(); return liste_initiale; } ``` It's a list of objects `Tache` in which I'd like to use it's three fields `Tache_description`, `Begin_date` and `End_date`. In my JavaScript code I have this function and it works fine: ``` <script> $(document).ready(function () { var date = new Date(); var d = date.getDate(); var m = date.getMonth(); var y = date.getFullYear(); $('#calendar').fullCalendar({ theme: true, header: {left: 'prev,next today',center: 'title',right: 'month,agendaWeek,agendaDay'}, editable: true, events: [ @foreach (var m in Model.Get_List_Tache()) { @:{title : @m.Tache_description , start: @m.Begin_date , end : @m.Begin_date } } ] }); }); </script> ``` The values of the array `events` are just for test, and I need to fill `events` by the value of the `Model`. For each element like this: `title = Tache_description`, `start = Begin_date` and `end = End_date`. So how can I do this task? Any suggestions?

Original source