简体   繁体   English

我们如何将javascript var初始化为c#var Asp.net MVC Razor View

[英]how we initialize javascript var to c# var Asp.net MVC Razor View

I want to save UserName in to valC# 我想将UserName保存到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# 请告诉我们如何将值从Java脚本变量保存到C#变量

</script>

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. 如果需要将值从客户端(JavaScript)推送到服务器端(您的C#代码在服务器上执行),则需要以某种方式将其发送到服务器。

$(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. 这会将输入的用户名发布到激活当前视图的同一控制器上的Action SaveUserName上。 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM