我想在View端使用Controller中的数据javascript

I want to use the data in the Controller in the View side javascript

这是我的控制器:

public ActionResult AjaxSmsSend(Sms smsInfo)
        {
            var sms = smsInfo.smsCode;
            var telephone = smsInfo.telephone;
            ViewBag.Code = sms;  
            return Json(sms); 
        }

在视图中:

<button id="getDataBtn">Click me</button>

当我按下这个按钮时:

<script type="text/javascript">
    $(function () {
        $("#getDataBtn").click(function () {
            $.ajax({
                type: "GET",
                url: "/Home/AjaxSmsSend",
                data: sms,
                contentType: "application/json; charset=utf-8",
                dataType: "json", 
        });
    });
</script>

我会用它来比较我从控制器收到的短信数据。

<p style="text-align: center;margin-top: 10%;">Enter your sms code</p>
<input type="text" id="pincode" maxlength="4">

此输入由用户输入。我已经在控制器本身生成了短信数据。
我敢肯定这实际上是一个非常简单的过程。但是因为我刚刚开始,我找不到我要找的东西。如果你能帮助我,我将不胜感激。

我认为这不是一个好习惯,通常应该在服务器端进行比较,但您可以添加:

 success: function (response) {
                    if (response.smsCode)
                    {                     

                    }
                }


会变成这样:

<script type="text/javascript">
    $(function () {
        $("#getDataBtn").click(function () {
            $.ajax({
                type: "GET",
                url: "/Home/AjaxSmsSend",
                data: sms,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    if (response.smsCode)
                    {                     

                    }
                }
        });
    });
</script>

如果 :

然后把你的代码放在里面
if (response.smsCode)
                    {                     

                    }

就这些了,希望对您有所帮助