Choose Modal from variable

Asked

Viewed 132 times

1

I am giving the value to a variable string in my index.Cs file and then I am taking the value to use in Typescript, however I am creating another page besides index now and is giving error when I call the variable to be used in TS, i would like to take the variable of a specific model, see how I am doing:

<script type="text/javascript"> var mensagem = "@Model.msg";</script>

but this way is charging the other Model this variable, and there is no, so I would like to take only the index, that would be +/- in this sense, but not exactly how to do:

<script type="text/javascript"> var mensagem = "@Model.IndexModel.msg";</script>

if anyone can help me, I appreciate.

  • If possible DIT the question by adding more details about how you are doing, and where is this code snippet from the example.

  • 1

    Couldn’t you create a base class with that information and inherit all the models you’ll need? so I would have the information in various models and I would solve this

3 answers

1

To do this would not be by the Model in the View, you need to use Viewbag or Viewdata to set the information you need in your Onpost() and receive it in your other View.

Example:

Controller

public IActionResult OnPost() {
    ViewBag.MsgErro = "Mensagem de erro";
}

View

<script type="text/javascript">
    var mensagem = '@ViewBag.MsgErro';
    alert(mensagem);
</script>

If you would like a further explanation of the operation of the Viewbag and Viewdata contents of Eduardo Pires.

0

The easiest way to do this is through the session

EXAMPLE:

Criando uma Session na sua pagina index.cs: 
Session["nomeSession"] = "aqui você pega valor que você deseja passar pra outra pagina"; 

Lendo uma Session: 
String strValorSession = Session["nomeSession"]; 

more information on Lik: https://www.codigofonte.net/dicas/csharp/80_trabalhando-com-session-em-c

0

I worked little with ASP.NET, but it should be something similar to what you typed. As far as I know, to get a model variable, you should use

@Model."Nome do Model"."Nome da variável";

But without using quotation marks, I identified in another part of the code, something similar to this:

@{var price=50;}
<html>
<body>
@if (price>30)
{
<p>The price is too high.</p>
}
</body>
</html> 
  • So it would be like I put it there anyway, right?? var mensagem = "@Model.IndexModel.msg";, but unfortunately it’s not working... I also thought it would be like this, but it didn’t work. :/

  • ASP.NET works in a similar way to Javascript, where you will have a separate part of the code to define these functions

  • That wouldn’t work for me... What happens is this, within my Onpost() method, I call another method, which returns me a string, if any error occurs, and if it does not occur, it will not return any of this string... it will just proceed to the next step. and depending, if you return the string, I should show it to the user and not go to the next page... It is a login screen right... so I check the data, and depending on it can not access

Browser other questions tagged

You are not signed in. Login or sign up in order to post.