Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
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>
Posted
Updated 3-Aug-17 2:36am
Comments
F-ES Sitecore 19-Jul-17 4:38am    
If it's not working then there is something about the flow of code that you're not telling us. The alert will actually always show if you have anything in TempData["notice"] or not.
Vincent Maverick Durano 19-Jul-17 23:10pm    
Perhaps it has something to do with your if condition. Have you tried checking the value of your tempdata? Try alerting the value of your tempdata instead like:

alert('@TempData["notice"]');
GrpSMK 3-Aug-17 1:30am    
Thank you,i used boot box alert solved my issue

Because
TempData["notice"]
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 :)
 
Share this answer
 
Comments
GrpSMK 3-Aug-17 1:19am    
Yea,tried this method not working
[no name] 3-Aug-17 2:10am    
Inspect element by F12 and tell me the error from Console.
F-ES Sitecore 3-Aug-17 6:12am    
There's nothing wrong with the code, TempData was preceded by @ which means MVC renders the TempData value in the output js. If the "@" wasn't there your point would be correct. Secondly he is use the razor engine so "<% %>" notation doesn't apply
Try This, I hope this will resolve your problem. However, I would suggest using viewbag instead of TempData, the viewbag Doesn’t require typecasting.

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


Here is the Javascript code

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900