警报未在MVC中显示 [英] Alert not displaying in MVC

查看:94
本文介绍了警报未在MVC中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按钮后单击警告不显示。我使用临时数据显示警报。



我尝试过:



控制器

After button click alert not displaying.Here i m using temp data for displaying alert.

What I have tried:

Controller

[HttpPost]
    public ActionResult PlanUpdate(string stripeToken, string stripeEmail, string btnSubmit, FormCollection form,  UpdatePlan model)
    {
        string SubscriptionId = Session["SubsId"].ToString();
        var btnValue = form["btnSubmitValue"];
        string apiKey = "****";
        var stripeClient = new StripeClient(apiKey);

        var subscriptionService = new StripeSubscriptionService(apiKey);
        var subscriptionService1 = new StripeSubscriptionUpdateOptions();
        subscriptionService1.PlanId = btnValue;
        subscriptionService1.Prorate = true;
        subscriptionService1.ProrationDate = System.DateTime.Now;
         StripeSubscription stripeSubscription = subscriptionService.Update (SubscriptionId, subscriptionService1);

        TempData["notice"] = "Successfully registered";

        return View("Index");
    }



查看页面


View Page

<script>

    if ('@TempData["notice"]' != null) {
        alert("success");

    }
</script>

推荐答案

因为
TempData["notice"]

无法直接在Javascript中访问。因为代码的ASP.NET部分在服务器上执行。 JavaScript在浏览器中执行。服务器只是将JavaScript视为文本,它没有意义且无法使用。只有当浏览器收到页面并解释JS时才会执行。



尝试使用一些链接来访问javascript中的c#变量。



is not accessible in Javascript directly. Because ASP.NET parts of the code execute on the server. JavaScript is executed in the browser. The server just sees the JavaScript as text, it is meaningless and non-functional. Only when the browser receives the page and interprets the JS is it executed.

Try to use something link that, to access the c# variable in javascript.

<script>
var myArray = <% = new JavaScriptSerializer().Serialize(TempData) %>;
if(myArray['notice']!=null){
alert("Success");
} 
</script>





希望它对您有所帮助,请不要忘记标记我的好评。谢谢:)



Hope it will helpful for you, please don't forget to mark my good rating. Thanks :)


试试这个,我希望这能解决你的问题。但是,我建议使用viewbag而不是TempData,viewbag不需要进行类型转换。



Try This, I hope this will resolve your problem. However, I would suggest using viewbag instead of TempData, the viewbag Doesn’t require typecasting.

public ActionResult About()
     {
         ViewBag.Message = "Your application description page.";
         TempData["notice"] = "Successfully registered";
         return View("Index");
     }





这是Javascript代码





Here is the Javascript code

var temp='@(TempData["notice"])';
    if(temp !=null)
    {
        alert('sucsecc');
    }


这篇关于警报未在MVC中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆