0

I want to save UserName in to valC#

@{
var valC#;
}

<script>
    $(document).ready(function () {
        var UserName = $(".dir span").text();
        UserName.trim().replace("\n", "");

        alert(UserName);
    });

Please tell how to save value from java-script variable to a variable of C#

</script>
1

1 Answer 1

1

If you need to push a value from client side (JavaScript) to server side (your C# code is executed on the server) you need to send it to the server in some fashion.

$(document).ready(function () {
    var UserName = $(".dir span").text();
    UserName.trim().replace("\n", "");

    $.post("@Url.Action("SaveUserName")", { username: UserName });
});

This will post the entered username to the Action SaveUserName on the same controller that your current view was activated from. So on your controller you will need the following Action

public ActionResult SaveUserName(string username)
{
    // This is where you would save the username on the serverside
}
Sign up to request clarification or add additional context in comments.

2 Comments

I think you mean '@Url.Action("SaveUsername")'.
Indeed, that is what I meant. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.