Ayyaayaa
Ayyaayaa

Reputation: 41

How to create array from Model?

Im new to javascript and I have this code where I should pass or convert to jQuery. I have tried some code but it doesn't work. How can I do it properly?

Here is what it should look like:

<script>
  $('#calendar').fullCalendar({
    defaultDate: '2017-10-12',
    editable: true,
    eventLimit: true, // allow "more" link when too many events
    events: [
        {
            title: 'Juan Dela Cruz',
            start: '2017-10-01T10:30:00',

        },
        {
            title: 'Juan Dela Cruz',
            start: '2017-10-12T10:30:00',

        },
        {
            title: 'Juan Dela Cruz',
            start: '2017-10-27T12:00:00'
        }
    ]
});

and here is what I have tried:

<script>
var x = [];
@foreach(var item in Model)
{
    @:x.push(title='@Html.DisplayFor(x=>item.Patient.LastName)', start='@Html.DisplayFor(x=>item.ScheduleDate)')
}
$('#calendar').fullCalendar({
    defaultDate: '2017-10-12',
    editable: true,
    eventLimit: true, // allow "more" link when too many events
    events: x
});

Upvotes: 3

Views: 143

Answers (2)

Amir H KH
Amir H KH

Reputation: 361

The statement foreach in jQuery is $.each and this a simple sample to do that :

$.each( obj, function( key, value ) {
  alert( key + ": " + value );
});

Upvotes: 2

Mihai Alexandru-Ionut
Mihai Alexandru-Ionut

Reputation: 48357

You're iterating correct the Model using @foreach statement.

The problem is at this line:

@:x.push(title='@Html.DisplayFor(x=>item.Patient.LastName)', start='@Html.DisplayFor(x=>item.ScheduleDate)')

You have to push an object in following way:

@:x.push({title:'@item.Patient.LastName', start:'@item.ScheduleDate'});

Upvotes: 5

Related Questions