如何在剃刀视图中使用Javascript分割(' @')脚本?

时间:2018-04-30 12:13:14

标签: javascript jquery razor

@{
ViewBag.Title = "Index";
Layout = null;
}
</head>
<body>
<!DOCTYPE html>
<html lang="en">
<head>
<div id="login-page">
    <div class="container">

        <div class="form-login">
            <h2 class="form-login-heading">sign in now</h2>
            <div class="login-wrap">
                <input type="text" id="txtUsername" class="form-control" 
placeholder="User ID" autofocus>
                <br />
                <input type="password" id="txtPassword" class="form-control" 
placeholder="Password">
                <label class="checkbox">
                    <span class="pull-right">
                        <a data-toggle="modal" href="login.html#myModal"> 
Forgot Password?</a>

                    </span>
                </label>
                <button class="btn btn-theme btn-block" id="btnLogin"><i 
class="fa fa-lock"></i> SIGN IN</button>
                <hr>


                <div class="registration">
                    Don't have an account yet?<br />
                    <a class="" href="#">
                        Create an account
                    </a>
                </div>

            </div>
        <div id="divError" class="alert alert-danger collapse">
            <a id="linkClose" href="#">&times;</a>
            <div id="divErrorText">

            </div>
        </div>
    </div>
</div>
<script>
$('#btnLogin').click(function () {
                $.ajax({
                    url: '/token',
                    method: 'POST',
                    contentType: "application/json",
                    data: {
                        username: $('#txtUsername').val(),
                        password: $('#txtPassword').val(),
                        grant_type: "password"
                    },
                    success: function (response) {
                        localStorage.setItem('accessTokenP', 
response.access_token);
                        //example@yahoo.com

                        var x = response.userName.toString().split('@'); 
                        localStorage.setItem('NameP', x[0]);

                        $('#divErrorText').text(JSON.stringify(response));
                        $('#divError').show('fade');
                        var url = "/Home/index";
                       window.location.href = url;
                    },
                    error(response) {
                        alert("error")
                        $('#divErrorText').text(JSON.stringify(response));
                        $('#divError').show('fade');

                    }
</script>

</body>

</html>

来源错误:

第128行:成功:函数(响应){ 第129行:localStorage.setItem(&#39; accessTokenP&#39;,response.access_token); 第130行:var x = response.userName.toString()。split(&#39; @&#39;); 第131行:localStorage.setItem(&#39; NameP&#39;,x [0]); 第132行:

源文件:/Views/Home/Login.cshtml行:130

1 个答案:

答案 0 :(得分:0)

由于@是Razor中的保留字符,要在字符串中获取实际的@字符,您需要通过加倍来转义@con。所以

   var x = response.userName.toString().split('@'); 

变为

 var x = response.userName.toString().split('@@'); 

通过这种方式逃避它,Razor明白你使用@ not作为Razor符号,但是作为文字和单个@将放在字符串中,这就是你需要的。