frankcollins3 / ASPokedex

Pokedex with ASP backend / razor pages, and VanillaJS front end
1 stars 0 forks source link

variable name does not exist in the current context (model from codebehind) [7:27pm] #8

Closed frankcollins3 closed 1 year ago

frankcollins3 commented 1 year ago

attempting to do: access the Model created in the code-behind file, of which there are 3 public <list> string created from the pokeAPI: 1 (pokemon.name) 2 (pokemon.sprites.front_default) // front facing img-src-data 3 (pokemon.sprites.back_default) // back facing img-src-data Screen Shot 2023-09-19 at 7 23 06 PM

error: Screen Shot 2023-09-19 at 7 23 13 PM

proposed approach: 0: first approach includes accessing the by index, determined by pokemonId // should also include [i] - 1 but still doesnt explain error.

possible improvements:

frankcollins3 commented 1 year ago

fixed by redeclaring the @Model.PokemonSrc as a variable returned with @Html.Raw(Json.Serialize):

<script>
👍 var pokemonSrcBack = @Html.Raw(Json.Serialize(Model.PokemonSrcBack));
👍 var pokemonSrc = @Html.Raw(Json.Serialize(Model.PokemonSrc));

const pokemonIdFromSrc = (src) => {
        if (typeof src === "string") 
    {
        let srcId = src.replace(/\D+/g, '');
        return srcId
    } else
    {
        return;
    }
}

const pokeMouseEnter = (event) => {
    console.log('event', event);
    let src = event.target.src;
    let srcId = pokemonIdFromSrc(src);
    $(event.target).attr('src', pokemonSrcBack[srcId - 1]);
}

const pokeMouseLeave = (event) => {
    let src = event.target.src;
        if (typeof src === "string")
    {
        let srcId = pokemonIdFromSrc(src);
        $(event.target).attr('src', pokemonSrc[srcId - 1]);
    } else 
    {
        return;
    }
}

const pokemonClick = (event) => {
    console.log('event', event)
    let src = event.target.src
    let srcId = src.replace(/\D+/g, '');
    console.log(src);
    console.log(srcId);
}

</script>