How to pass a value to razor variable from javascript variable?

后端 未结 7 931
小鲜肉
小鲜肉 2020-11-27 19:43

How to pass a value to razor variable from javascript variable, is it possible asp.net mvc razor view engine?

@{
    int a = 0;
}



        
相关标签:
7条回答
  • 2020-11-27 20:31

    I see that this problem was discussed some time ago, but if anyone 'll meet with this again, here is my solution:

    In your *.cshtml View file:

    <script>
    var data = JsFunction("@Html.Raw(Model.Path)");
    $(function () {
        $("#btn").click(function () {
            var model = { Id: '@Html.Raw(Model.Id)', Data: data }
            $.ajax({
                type: "POST",
                url: "/Controller/Action",
                data: model,
                datatype: "html",
                success: function() {
                    console.log('Success');
                }
            });
        });
    });
    </script>
    

    JavaScript variable model is something that I need to pass to Razor ViewModel. It can be done with ajax request. You just need to have proper argument/s in your action, that matches Json object created in JavaScript.

    Hope it'll help someone!

    0 讨论(0)
  • 2020-11-27 20:33

    Step: 1 Your Html, First Store the value in your localstorage using javascript then add the line like below ,this is where you going to display the value in your html, my example is based on boostrap :

    <label for="stringName" class="cols-sm-2 control-
    label">@Html.Hidden("stringName", "")</label>
    

    Step:2 Javascript

     $('#stringName').replaceWith(localStorage.getItem("itemName"));
    
    0 讨论(0)
  • 2020-11-27 20:37

    But it would be possible if one were used in place of the variable in @html.Hidden field. As in this example.

    @Html.Hidden("myVar", 0);
    

    set the field per script:

    <script>
    function setMyValue(value) {
         $('#myVar').val(value);       
    }
    </script>
    

    I hope I can at least offer no small Workaround.

    0 讨论(0)
  • 2020-11-27 20:38

    here is my solution that works:

    in my form i use:

    @using (Html.BeginForm("RegisterOrder", "Account", FormMethod.Post, new { @class = "form", role = "form" }))
      {
    
       @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
    
       @Html.HiddenFor(m => m.quantity, new { id = "quantity", Value = 0 })
    
      }
    

    in my file.js I get the quantity from a GET request and pass the variable as follows to the form:

        $http({
            method: 'Get',
            url: "https://xxxxxxx.azurewebsites.net/api/quantity/" + usr
        })
            .success(function (data){
    
                setQuantity(data.number);
    
                function setQuantity(number) {
                    $('#quantity').val(number);
                }
    
            });
    
    0 讨论(0)
  • 2020-11-27 20:39

    You can't. and the reason is that they do not "live" in the same time. The Razor variables are "Server side variables" and they don't exist anymore after the page was sent to the "Client side".

    When the server get a request for a view, it creates the view with only HTML, CSS and Javascript code. No C# code is left, it's all get "translated" to the client side languages.

    The Javascript code DO exist when the view is still on the server, but it's meaningless and will be executed by the browser only (Client side again).

    This is why you can use Razor variables to change the HTML and Javascript but not vice versa. Try to look at your page source code (CTRL+U in most browsers), there will be no sign of C# code there.

    In short:

    1. The server get a request.

    2. The server creates "takes" the view, compute and translate all the C# code that was embedded in the view, to CSS,Javascript, and HTML.

    3. The server returns the client side version of the view to the browser as a response to the request.
    4. the browser renders the page and executes all the Javascripts
    0 讨论(0)
  • 2020-11-27 20:44

    Razor View Server Side variable can be read to Client Side JavaScript using @ While and JavaScript client side variable can read to Razor View using @:

    0 讨论(0)
提交回复
热议问题