Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access javascript variable inside razor [duplicate]

Is it possible to access the javascript variable inside razor syntax?

Here is my code

       $("#add-user-to-plan").click(function () {
            var userListId = $("#user-list").val();                
            @{
                Model.Users.Add(userListId );
            }

       });

When I click the add-user-to-plan button it will add an item to a list of Users.

like image 419
Ramppy Dumppy Avatar asked Feb 03 '26 18:02

Ramppy Dumppy


1 Answers

In your Razor create a hidden field

  @Html.HiddenFor(model => model.userListId , new { id = "userListIdHidden" });

In java script assign the value for userListIdHidden

   $("#add-user-to-plan").click(function () {
        var userListId = $("#user-list").val();                
        document.getElementById("userListIdHidden").value = userListId;
   });
like image 54
Golda Avatar answered Feb 05 '26 07:02

Golda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!