I want to pass data from controller to javascript file. Here is my model Field
[Key]
public int id_field { get; set; }
public string name { get; set;
public int field_type { get; set; }
This is my GameModel
public class GameModel
{
public List<Board> board { get; set; }
public List<Player> player_list { get; set; }
public List<Dice> dices_value { get; set; }
public List<Field> field_list { get; set; }
}
and here is javascript code:
<script>
function setup() {
createCanvas(880, 880);
background(255);
for (var i = 0; i < 11; i++) {
var posX = map(i, 0, 11, 0, width);
var posY = map(i, 0, 11, 0, height);
var posX2 = map(i, 0, 11, 0, width);
var posY2 = map(i, 0, 11, height, 0);
var tileRowUp = new Tile(posX, 0, 80, 80);
if (i == 1) {
tileRowUp.nameOfField=@Html.Raw(Json.Serialize(Model.field_list[1].name));
}
if (i == 2) {
tileRowUp.nameOfField=@Html.Raw(Json.Serialize(Model.field_list[2].name));
}
tileRowUp.show();
var tileColLeft = new Tile(0, posY, 80, 80);
if (i == 0) {
tileColLeft.nameOfField=@Html.Raw(Json.Serialize(Model.field_list[0].name));
}
tileColLeft.show();
var tileRowdown = new Tile(posX2, height - 80, 80, 80);
tileRowdown.show();
var tileColRight = new Tile(width - 80, posY2, 80, 80);
tileColRight.show();
}
var mysteriousCard1 = new Tile(170, 190, 100, 100);
mysteriousCard1.show();
var mysteriousCard2 = new Tile(570, 490, 100, 100);
mysteriousCard2.show();
var dice = new Tile(390, 390, 50, 50);
dice.show();
}
class Tile {
constructor(x, y, lar, alt, id_Field,nameOfField,TypeOfField) {
this.x = x;
this.y = y;
this.lar = lar;
this.alt = alt;
this.id_Field = id_Field;
this.nameOfField = nameOfField;
this.TypeOfField = TypeOfField;
}
show() {
//noStroke();
rect(this.x, this.y, this.lar, this.alt);
text(this.nameOfField, this.x + 10, this.y + 10);
}
}
Using tileColLeft.nameOfField=@Html.Raw(Json.Serialize(Model.field_list[0].name));
I assign data to object name tileColLeft. Here I mix js code and HTML and I do not want to do this.
This work, it display me data from database.
Here is result
https://i.stack.imgur.com/h0oEr.jpg
Also script javascript is now in my .cshtml file . I have to make this js code to be external file. I thought about using JsonResult but I cannot understand how can I should do this. Somebody have some suggestion?