❔ MVC Tuple to View problem
Is
Item2
the tuple? Or is Model
the tuple? What is it a tuple of?49 Replies
Also, why not just use a class? Or a record, even?
i think in your 2nd loop you dont want a @ in front of Model
This alert in my code brings back the value of the of first occurrence. in the for loop it does not like the variable index? What is wrong with this?

wait is that javascript or c#
javascript
being passed from c# MVC Controller
well, what is your model exactly because if @model.Item2 works that means your model is a single tuple, not a list
oh your item2 is a list
correct
If I hard code the index it works fine...
yea im trying to think how to pass the javascript index into the c#
the thing is the c# writes the javascript
so i dont think you can do that
basically once the view renders the c# doesnt exist anymore its just the resulting javascript
It is coming back fine from the C# controller. It is iterating thru it that is my issue ..
you have to set a javascript variable to equal your entire model.item2
then you can iterate through that javascript vaariable
could you show? I don't understand
well the thing is im not so sure how to do it
The data is in the Tuple model... How do I iterate it?
it would be something like
var list = @model.item2
for (var i = 0; i < 3; i++){
alert(list[i].Longitude)
}
but there might be something extra to add
hold on
var list = @Html.Raw(Model.Item2)
i think that's what's missing
Getting closer.... the loop with jsonmodel is returning undefined...

you can inspect in the browser to see what javascript is actually rendered
sorry ... kind of a newbee. How do I watch this come thru. I have inspect up.....
in sources
you have to provide screen shot or i cant help

lol try to find your function
in that
hold on i made a mvc page with basically same model im gonna check
im getting this javascript with code similar to yours
<script>
var model = [{"longitude":34,"potatoName":"tom"},{"longitude":66,"potatoName":"pat"},{"longitude":11,"potatoName":"joe"}];
for (var i=0; i< 3; i++){
alert(model[i].Longitude)
}
</script>
do this to see what the model actually is
You still haven't showed us how it looks
Just that it's a tuple, and we can infer
Item2
is some sort of a collectionbut i did same thing as him basically and the rendered javascript is shown above but the alert still gives me undefined too hmm
longitude
!== Longitude
Case sensitivityoh yea, then that must be his problem too

just change Longitude to longitude and youre good Danno
I have case correct. Model values are all there if I hard code in alert statement. It is something with the indexing....
no look
<script>
var model = @Html.Raw(Json.Serialize(Model.Item2));
for (var i=0; i< 3; i++){
alert(model[i].longitude)
}
</script>
Yeah, aight, you refuse to give any details so I'm out
this is my potato class
public class Potato
{
public int Longitude { get; set; }
public string PotatoName { get; set; }
}
Good luck solving the issue by saying not much more than "thing no work"
Longitude is capitalized in c# but not in js
in my case Model.Item2 is a list of potato
public IActionResult Index()
{
Tuple<int,List<Potato>> model = new Tuple<int,List<Potato>>(1,new List<Potato>() { new Potato() { Longitude = 34, PotatoName="tom" }, new Potato() { Longitude = 66, PotatoName = "pat" }, new Potato() { Longitude = 11, PotatoName = "joe" } });
return View(model);
}
so yea just write longitude with lowercase l
Protip: just use
(int, List<Potato>)
instead of the old Tuple<>
syntaxoh yea forgot bout that
You can also get rid of those ugly-ass default
Item1
Item2
names as well with it
(number: 7324, taters: new List<Potato(){ new(), new(), new() })
Yes... Lower case takes care of it !!! In the Model it is upper case. How in the hell is someone supposed to know that?
haha you have to inspect the javascript to figure it out
javascript is really stupid, there are tons of traps like that
If you could still point me to where I can see that in inspector. That would really help. Sorry I am such a nob ass.
well i dont know what your page is called
its in the html of that page
in the script section
Angius#1586
do this to see what the model actually is
Quoted by
<@!85903769203642368> from #MVC Tuple to View problem (click here)
React with ❌ to remove this embed.
see for me its the index page so i see it in the index file

i can see iut in the elements tab too

that's all the help i can give, about that matter
Hey... Thanks alot! I see how those values are displayed now in Inspector. Thank you for taking the time with a newbie!!! You have done a good deed for the day!!! Have a great one!
For the loop.... How do I get the number of records in model? I have tried model.count... doesn't work... What is the easiest way to get this value?
in javascript its lenght
length
right on Again! Your the Master!
no problem
Or a
for (const thing of things)
loop, the equivalent of C#'s foreach
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.