3

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.

1

1 Answer 1

5

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;
   });
Sign up to request clarification or add additional context in comments.

1 Comment

Bad solution. Get exception async...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.