C
C#15mo ago
0x05962

❔ stockticker prints good but on the website viewbag.stockticker is nil, why is that?

38 Replies
Angius
Angius15mo ago
Why are you using ViewBag and not typed models?
0x05962
0x05962OP15mo ago
Simplier but holdon could it be my javascript
0x05962
0x05962OP15mo ago
0x05962
0x05962OP15mo ago
like the redirection? if not ima try the strong typed models
Angius
Angius15mo ago
Well, your view won't update just because you set some variable in the backend, if that's what you're trying to do If it's rendered server-side, like a view, it's rendered, sent to the browser and that's it To update the data you would have to reload the page And make sure whatever new data you want is added during that request Or you can use JS to avoid the reload
0x05962
0x05962OP15mo ago
I am using js I don't understand much but a model would fix this?
Angius
Angius15mo ago
But also render things server-side?
0x05962
0x05962OP15mo ago
I thought the server would send it to the view
Angius
Angius15mo ago
It would make more proper code, but no, it's unlikely to solve the issue
0x05962
0x05962OP15mo ago
oh
Angius
Angius15mo ago
Your problem stems from the fundamental misunderstanding how websites work, what's done on the server, what's done on the client, and so on
0x05962
0x05962OP15mo ago
So I can use js to get the viewbag??
Angius
Angius15mo ago
No JS has no concept of a viewbag
0x05962
0x05962OP15mo ago
ik but like a get request to the controller
Angius
Angius15mo ago
Sure, that you can do
0x05962
0x05962OP15mo ago
How would you do it I think my code is poorly structured as I have not used one query 💀
Angius
Angius15mo ago
You would have an API controller
[ApiController]
[Route("[controller]")]
public class SomethingController : ControllerBase
{
[HttpGet]
public ActionResult<int> GetRandomNumber()
{
var rand = Random.Shared.Next(0, 100);
return Ok(rand);
}
}
[ApiController]
[Route("[controller]")]
public class SomethingController : ControllerBase
{
[HttpGet]
public ActionResult<int> GetRandomNumber()
{
var rand = Random.Shared.Next(0, 100);
return Ok(rand);
}
}
something like this That you would then call with JS
const res = await fetch('/something');
const num = await res.text();
alert('Your number is ${num}');
const res = await fetch('/something');
const num = await res.text();
alert('Your number is ${num}');
0x05962
0x05962OP15mo ago
Is there something I can use to store data across controllers and not use database
Angius
Angius15mo ago
A file, if you really want But a database would be ideal
0x05962
0x05962OP15mo ago
like literally build a mongodb for this stuff? or would cookies do
Angius
Angius15mo ago
You don't need to build anything And cookies are client-side You can use SQLite with EF Core Or LiteDB
0x05962
0x05962OP15mo ago
also what does return RedirectToAction("Data", "Stock"); do in the controller
Angius
Angius15mo ago
It redirects to another action
0x05962
0x05962OP15mo ago
so it'd call the IActionResult Data()?
0x05962
0x05962OP15mo ago
Chatgpt said to do it to pass the viewbag
Angius
Angius15mo ago
Well, ChatGPT is very often confidently incorrect So
0x05962
0x05962OP15mo ago
so if somehow I did a post request from the browser to the server right and the controller returns a view the browser view will change?
Angius
Angius15mo ago
If you do it with a form or a link, yes If you do it with fetch, XHR or ajax, no
0x05962
0x05962OP15mo ago
lol holdon so the viewbag goes where then
Angius
Angius15mo ago
Viewbag is, tentatively, used to pass data between the controller and the view And, sometimes, between views and layouts
0x05962
0x05962OP15mo ago
and models can be passed to js?
Angius
Angius15mo ago
Yes, they can be returned from a controller instead of a view
0x05962
0x05962OP15mo ago
cause ig it's not dynamic ohh Also when is it appropiate to have like this on your website
0x05962
0x05962OP15mo ago
Angius
Angius15mo ago
Uh, when you want to? You can make a complete website that never changes the address bar All navigation is done with JS A so-called SPA, or Single Page Application But you sometimes want people to, dunno, send eachother links to specific videos on youtube Or to a privacy policy Or something So... then
0x05962
0x05962OP15mo ago
mb i meant like the {id}
Accord
Accord15mo 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.
Want results from more Discord servers?
Add your server