在C#方法调用中嵌入Html`alert()`以显示警报窗口MVC

在C#方法调用中嵌入Html`alert()`以显示警报窗口MVC,c#,html,asp.net-mvc,razor,C#,Html,Asp.net Mvc,Razor,我试图在@functions{}代码块中添加Html代码,如果它符合某些条件,它将向用户发出警告 这就是我到目前为止所知道的,我得到了错误CS0103:当前上下文中不存在名称“alert” 这是抛出错误的代码段 @functions{ void doSomething(){ if(ViewBag.token != null){ @alert("@ViewBag.token"); } } } 有没有办法将

我试图在
@functions{}
代码块中添加Html代码,如果它符合某些条件,它将向用户发出警告

这就是我到目前为止所知道的,我得到了错误
CS0103:当前上下文中不存在名称“alert”

这是抛出错误的代码段

@functions{

    void doSomething(){

        if(ViewBag.token != null){
                    @alert("@ViewBag.token");
        }
    }
}
有没有办法将alert()嵌入.cshtml中的C#代码块中

这是该函数所在的全部代码

@using System.Web.Mvc
@using System.Web.Mvc.Html
@using System
@using System.Web.UI
@model Dependency_Injection_MEF_MVC.Models.Payment

@section Scripts{
    <script type="text/javascript" src="https://js.stripe.com/v2/"></script>
    <script type="text/javascript">
        Stripe.setPublishableKey('pk_test_6pRNASCoBOKtIshFeQd4XMUh');
    </script>

    <script type="text/javascript">
        $(function () {
            var $form = $('#payment-form');
            $form.submit(function (event) {
                // Disable the submit button to prevent repeated clicks:
                $form.find('.submit').prop('disabled', true);

                // Request a token from Stripe:
                Stripe.card.createToken($form, stripeResponseHandler);

                // Prevent the form from being submitted:
                return false;
            });
        });

        function stripeResponseHandler(status, response) {
            // Grab the form:
            var $form = $('#payment-form');

            if (response.error) { // Problem!

                // Show the errors on the form:
                $form.find('.payment-errors').text(response.error.message);
                $form.find('.submit').prop('disabled', false); // Re-enable submission

            } else { // Token was created!

                // Get the token ID:
                var token = response.id;

                ViewBag.token = token;

                // Insert the token ID into the form so it gets submitted to the server:
                $form.append($('<input type="hidden" name="Token">').val(token));

                // Submit the form:
                $form.get(0).submit();

            }
        };
    </script>
}

<div class="row">
    <div class="col-md-12 form-column">
        <div class="form-container">
            <form asp-controller="home" asp-action="processpayment" method="POST" id="payment-form">
                <span class="payment-errors"></span>

                <div class="form-group">
                    <h3>Membership Amount: USD XX</h3>
                </div>

                <div class="form-group">
                    <label for="cardNumber">Card Number</label>
                    <input class="form-control form-input" id="cardNumber" type="text" size="20" data-stripe="number" style= "width:250px;height:25px;font-size:120%">
                </div>

                <div class="form-group">
                    <label>Expiration (MM/YY)</label>
                    <div>
                        <input class="form-control form-input date-input" type="text" size="2" data-stripe="exp_month" style= "width:250px;height:25px;font-size:120%">
                        <input class="form-control form-input date-input" type="text" size="2" data-stripe="exp_year" style= "width:250px;height:25px;font-size:120%">
                    </div>
                </div>

                <div class="form-group">
                    <label for="cvc">CVC</label>
                    <input class="form-control form-input" id="cvc" type="text" size="4" data-stripe="cvc" style= "width:250px;height:25px;font-size:120%">
                </div>

                <input class="btn btn-default" onclick="doSomething()" id="submit" value="Submit Payment">
            </form>
        </div>
    </div>
</div>

@functions{

    void doSomething(){

        if(ViewBag.token != null){
                    alert("@ViewBag.token");
        }
    }

}
@使用System.Web.Mvc
@使用System.Web.Mvc.Html
@使用系统
@使用System.Web.UI
@模型依赖性\u注入\u MEF\u MVC.Models.Payment
@节脚本{
Stripe.setPublishableKey('pk_test_6pRNASCoBOKtIshFeQd4XMUh');
$(函数(){
var$form=$(“#付款单”);
$form.submit(函数(事件){
//禁用“提交”按钮以防止重复单击:
$form.find('.submit').prop('disabled',true);
//从Stripe请求令牌:
Stripe.card.createToken($form,stripeResponseHandler);
//阻止提交表单:
返回false;
});
});
函数stripeResponseHandler(状态、响应){
//抓取表格:
var$form=$(“#付款单”);
if(response.error){//问题!
//在表单上显示错误:
$form.find('.payment errors').text(response.error.message);
$form.find('.submit').prop('disabled',false);//重新启用提交
}否则{//令牌已创建!
//获取令牌ID:
var token=response.id;
ViewBag.token=令牌;
//将令牌ID插入表单,以便将其提交到服务器:
$form.append($('').val(标记));
//提交表格:
$form.get(0.submit();
}
};
}
会员金额:XX美元
卡号
有效期(年月日)
CVC
@功能{
无效剂量测定法(){
if(ViewBag.token!=null){
警报(“@ViewBag.token”);
}
}
}

功能完全是服务器端的。但该脚本可以很容易地嵌入;如果将其移动到要调用的页面,只需执行以下操作:

@if(ViewBag.token != null){
  <script>
     alert("@ViewBag.token");
  </script>
}
@if(ViewBag.token!=null){
警报(“@ViewBag.token”);
}

如果该标记存在,则会呈现该标记。这不需要函数;这可能在@helper中。

我想将其作为一个函数来执行,因为我希望在单击事件后
显示警报窗口,有没有办法嵌入一个警报,显示类似
的ViewBag元素?这可以实现吗?是的,它可以工作。如果按钮是“提交”按钮,则会出现对话框,然后页面将立即回发。