How to get a javascript value inside a c # ASP.NET block [closed]

-1

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

But since it has already been answered you do not have the possibility to do this within View, because C # runs on the server-side. The solution would be to process this all before returning the JSON, or using a Javascript library for this.

    
asked by anonymous 23.10.2017 / 20:52

2 answers

1

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

The C # code is for generating 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 HTML so that the client can receive it, it will no longer run until a new request is made to the server.

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

It might be interesting to read: What is a "stateless protocol" such as HTTP?

    
23.10.2017 / 21:11
0

As far as I know, you can only do this by running a submit for a method inside C #. For this you can create a form variable and call the submit method, passing the values you want per 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 javascripts frameworks on the market to do this too, like jQuery, AngularJS and others.

    
23.10.2017 / 21:14