How to receive a javascript value within a c# ASP.NET block

Asked

Viewed 342 times

-1

What I’d like to do is convert a value from within a JSON to Double and apply the String.Format("{0:N}", valor_JSON)); so I’d be converting 1000 -> 1.000.

However, as already answered, it is not possible to do this inside the View, because C# runs on the server side. The solution would be to process all this before returning JSON, or using a Javascript library for this.

2 answers

1

This is not possible the way you show it. C# runs on server, and Javascript wheel on the client (browser).

The C# code is used to generate the HTML (or CSS or JS) that will be sent to the browser. Therefore, once the C# code has been executed and has done its job of generating an HTML so that the client can receive it, it will no longer be executed until a new request is made to the server.

I couldn’t understand the purpose of the code, so I can’t tell you what to do. Maybe if you give a context and explain what you want to do, I can give you a solution in code.

It might be interesting for you to read: What is a "stateless protocol", like HTTP?

  • LINQ, I want to take a variable from my returned JSON, convert to Double to apply the String.Format("{0:N}", ValorRecebidoDoJSONAqui)); so I can convert type 1000 to 1,000. But apparently I will have to do this before returning JSON.

0

As far as I know you can only do this by running a Submit for a method within C#. For this you can create a form variable and call the Submit method, passing the values you want by parameter. Example:

var myForm = document.createElement("form");
myForm.action = 'url do seu método no C#';
myForm.method = "post";

//Setando o parâmetro
var input = document.createElement("input");
input.type = "text";
input.value = obj[key];
input.name = key;
myForm.appendChild(input);

//Adiciona o form ao corpo do documento
document.body.appendChild(myForm);
//Envia o formulário
myForm.submit();

Remembering that you can use market frameworks to do this too, such as jQuery, Angularjs and others.

Browser other questions tagged

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