0

I'm trying to pass an array from my MVC Model into a javascript array (to use in fullCalendar), however having an issue.

From the below, "myArray" is being populated with the correct amount of items from the model array, but when I try to show obj.DateTime (valid field within the MVC model array) it returns undefined. The undefined alert appears the correct amount of times for items in the array.

Any help at all would be appreciated.

var myArray = [];
@foreach (var d in Model.Appointments)
{
 @:myArray.push("@d");
}
myArray.forEach(function (obj) {
 alert(obj.DateTime);
})
deceze
524k88 gold badges804 silver badges952 bronze badges
asked Aug 20, 2018 at 11:36
7
  • 2
    What is this @ syntax...? Commented Aug 20, 2018 at 11:37
  • razor syntax. is it incorrect? Commented Aug 20, 2018 at 12:05
  • I don't know. We just need to know what it is. Because it's neither javascript nor model-view-controller. Commented Aug 20, 2018 at 12:06
  • I saw it here: stackoverflow.com/questions/23781034/… Commented Aug 20, 2018 at 12:06
  • Please don't mix JavaScript and C# together. The code you have written is not correct. Commented Aug 20, 2018 at 12:14

1 Answer 1

0

You can't use @d for converting your C# object to java-script object directly. You should encode your C# object to make a json value.

This code will encode (Serialize) your C# object to a JSON model.

@Html.Raw(Json.Encode(d));

Here is another question like yours

answered Aug 20, 2018 at 12:17

3 Comments

Thanks for your reply. Unfortunately I've been trying stuff like this to no avail. Can you give me more information on that? Model.Appointments is my array. Do you mean something like this? @foreach (var d in Model.Appointments) { @:myArray.push(@Html.Raw(Json.Encode(d))); }
Still gives me "undefined" for the correct amount if items in the array :(
Do you just have issue with DateTime property? You can solve your date converting problem with this post: stackoverflow.com/questions/27314663/…

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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