3

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
});

3
  • 5
    api.jquery.com/each Commented Oct 24, 2017 at 8:13
  • 3
    This looks less a jQuery issue and more a Razor outputting issue. Commented Oct 24, 2017 at 8:14
  • You can use var x = @Html.Raw(Json.Encode(Model.Select(x => new { title = x.Patient.LastName, start = x.ScheduleDate) Commented Oct 24, 2017 at 8:49

2 Answers 2

5

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'});
Sign up to request clarification or add additional context in comments.

Comments

2

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.