C
C#3y ago
Angius

❔ MVC Tuple to View problem

Is Item2 the tuple? Or is Model the tuple? What is it a tuple of?
49 Replies
Angius
AngiusOP3y ago
Also, why not just use a class? Or a record, even?
PatrickG
PatrickG3y ago
i think in your 2nd loop you dont want a @ in front of Model
danno8524
danno85243y ago
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?
PatrickG
PatrickG3y ago
wait is that javascript or c#
danno8524
danno85243y ago
javascript being passed from c# MVC Controller
PatrickG
PatrickG3y ago
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
danno8524
danno85243y ago
correct If I hard code the index it works fine...
PatrickG
PatrickG3y ago
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
danno8524
danno85243y ago
It is coming back fine from the C# controller. It is iterating thru it that is my issue ..
PatrickG
PatrickG3y ago
you have to set a javascript variable to equal your entire model.item2 then you can iterate through that javascript vaariable
danno8524
danno85243y ago
could you show? I don't understand
PatrickG
PatrickG3y ago
well the thing is im not so sure how to do it
danno8524
danno85243y ago
The data is in the Tuple model... How do I iterate it?
PatrickG
PatrickG3y ago
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
danno8524
danno85243y ago
Getting closer.... the loop with jsonmodel is returning undefined...
PatrickG
PatrickG3y ago
you can inspect in the browser to see what javascript is actually rendered
danno8524
danno85243y ago
sorry ... kind of a newbee. How do I watch this come thru. I have inspect up.....
PatrickG
PatrickG3y ago
in sources you have to provide screen shot or i cant help
danno8524
danno85243y ago
PatrickG
PatrickG3y ago
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>
Angius
AngiusOP3y ago
<pre>
@JsonSerializer.Serialize(@Model, new JsonSerializerOptions { WriteIndented = true })
</pre>
<pre>
@JsonSerializer.Serialize(@Model, new JsonSerializerOptions { WriteIndented = true })
</pre>
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 collection
PatrickG
PatrickG3y ago
but i did same thing as him basically and the rendered javascript is shown above but the alert still gives me undefined too hmm
Angius
AngiusOP3y ago
longitude !== Longitude Case sensitivity
PatrickG
PatrickG3y ago
oh yea, then that must be his problem too
PatrickG
PatrickG3y ago
PatrickG
PatrickG3y ago
just change Longitude to longitude and youre good Danno
danno8524
danno85243y ago
I have case correct. Model values are all there if I hard code in alert statement. It is something with the indexing....
PatrickG
PatrickG3y ago
no look <script> var model = @Html.Raw(Json.Serialize(Model.Item2)); for (var i=0; i< 3; i++){ alert(model[i].longitude) } </script>
Angius
AngiusOP3y ago
Yeah, aight, you refuse to give any details so I'm out
PatrickG
PatrickG3y ago
this is my potato class public class Potato { public int Longitude { get; set; } public string PotatoName { get; set; } }
Angius
AngiusOP3y ago
Good luck solving the issue by saying not much more than "thing no work"
PatrickG
PatrickG3y ago
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
Angius
AngiusOP3y ago
Protip: just use (int, List<Potato>) instead of the old Tuple<> syntax
PatrickG
PatrickG3y ago
oh yea forgot bout that
Angius
AngiusOP3y ago
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() })
danno8524
danno85243y ago
Yes... Lower case takes care of it !!! In the Model it is upper case. How in the hell is someone supposed to know that?
PatrickG
PatrickG3y ago
haha you have to inspect the javascript to figure it out javascript is really stupid, there are tons of traps like that
danno8524
danno85243y ago
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.
PatrickG
PatrickG3y ago
well i dont know what your page is called its in the html of that page in the script section
MODiX
MODiX3y ago
Angius#1586
<pre>
@JsonSerializer.Serialize(@Model, new JsonSerializerOptions { WriteIndented = true })
</pre>
<pre>
@JsonSerializer.Serialize(@Model, new JsonSerializerOptions { WriteIndented = true })
</pre>
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.
PatrickG
PatrickG3y ago
see for me its the index page so i see it in the index file
PatrickG
PatrickG3y ago
i can see iut in the elements tab too
PatrickG
PatrickG3y ago
that's all the help i can give, about that matter
danno8524
danno85243y ago
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?
PatrickG
PatrickG3y ago
in javascript its lenght length
danno8524
danno85243y ago
right on Again! Your the Master!
PatrickG
PatrickG3y ago
no problem
Angius
AngiusOP3y ago
Or a for (const thing of things) loop, the equivalent of C#'s foreach
Accord
Accord3y ago
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.

Did you find this page helpful?