0

Timeline.js + MVC + JavaScript

I am having the following code on my view

@{           
    var objs = objstudent.Timelines.Where(x => x.StudentID == Sessions.objStudent.StudentID).AsQueryable();
}
<script type="text/javascript">
@{
    @:var data = [];
    foreach(Student.Models.Timeline student in objs)
    {
         @:data.push({start : new Date "@student.MeetingDate", content : "@student.Description" });
    }
}

here I am trying to add details in data array which will be used for displaying details in timeline, but when I check data's value in browser it shows me undefined.

I have tried using but problem is same.

any solution ?

1 Answer 1

1

Never build javascript literals manually like this. Always use proper serializers. For example:

@{           
    var objs = objstudent.Timelines.Where(x => x.StudentID == Sessions.objStudent.StudentID).AsQueryable();
}
<script type="text/javascript">
    var data = @Html.Raw(
        Json.Encode(
            objs.Select(x => new 
            { 
                start = x.MeetingDate, 
                content = x.Description 
            })
        )
    );
</script>

should end up with something along the lines of:

<script type="text/javascript">
    var data = [
        { "start": "\/Date(1334655137358)\/", "content": "foo" },
        { "start": "\/Date(1334655137358)\/", "content": "bar" },
        ...
    ];
</script>

Notice that the DateTime fields are serialized using the /Date(Ticks) format. You could use various techniques to convert them to native javascript dates.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.