0

Model.kanal is an array. I need a loop like this:

var boyut = @Model.kanal.Length;
    var knll = [];
    for (var i = 0; i < boyut; i++) {
        knll.push("@Model.kanal[i]"); //this i variable is not defined. Because it is a javascript variable. This is my problem.
    }

Thank you.

4
  • the short answer: you can not do this. Commented Sep 28, 2020 at 5:50
  • why you run JS loop on Model Array? Commented Sep 28, 2020 at 5:54
  • because I need to access the data from the model one by one and write on the screen Commented Sep 28, 2020 at 6:02
  • 1
    What makes you think that "You cannot do this" Mr.Gelichmann Commented Sep 28, 2020 at 6:14

3 Answers 3

3

You can get the array just like this:

var knll = @Html.Raw(Json.Serialize(Model.kanal));
Sign up to request clarification or add additional context in comments.

Comments

1

In your JS

var json = `@Json.Serialize(Model.YourArray)`;

var objects = JSON.parse(json);

console.log(objects);

1 Comment

No problem friend :)
1

try this

var boyut = @Model.kanal.Length; // Get Model Array Length

var knll = "@Html.Raw(Json.Serialize(Model.kanal))"; // Convert Array into Json and Set a Plan Text

for (var i = 0; i < boyut; i++) {
    knll.push(knll[i]);  // access From 
}

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.