0

on my view I have this code

<DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/css/toastr.min.css" rel="stylesheet" />
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.2/css/bootstrap.min.css" rel="stylesheet" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.min.js"></script>
    <script>
        function showContent() {
            toastr.options = {
                "closeButton": true,
                "debug": false,
                "progressBar": true,
                "preventDuplicates": false,
                "positionClass": "toast-top-right",
                "showDuration": "400",
                "hideDuration": "1000",
                "timeOut": "7000",
                "extendedTimeOut": "1000",
                "showEasing": "swing",
                "hideEasing": "linear",
                "showMethod": "fadeIn",
                "hideMethod": "fadeOut"
            }
            toastr["success"]("This is a message");

        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Button ID="Button1" runat="server" Text="Button" CssClass="btn btn-primary" OnClick="Button1_Click" />
        </div>
    </form>
</body>
</html>

But i like to start the toastr function from controller. For example the user fill out the form. Click on the "Safe" button and will redirect from the controller to the startpage

return RedirectToAction("Index", "Home");

now I like to show up the toastr notification on the startpage (with the message comes also from the controller).

Thanks for your help

Tony Ngo
  • 19,166
  • 4
  • 38
  • 60
Stefan
  • 555
  • 5
  • 18
  • 2
    You can't call client side code from server side code. Your use case looks like you may want to do the POST in javascript, do the toast based on failure or success, and then do the redirect in javascript `window.location.href = '@Url.Action("Index", "Home")';` Another strategy is to show an alert in your view like [this](https://stackoverflow.com/questions/13541225/asp-net-mvc-how-to-display-success-confirmation-message-after-server-side-proce) – Steve Greene Jan 21 '20 at 17:31

1 Answers1

0

You can use AddNodeServices in asp.net core like this

Add below line in the ConfiguraServices() in Startup.cs

public void ConfigureServices(IServiceCollection services)
{
   services.AddMvc();
   services.AddNodeServices();
}

Add constructor in the Controller

public class HomeController : Controller
    { 
        private readonly INodeServices _nodeServices;

        public HomeController(INodeServices nodeServices)

        {
            _nodeServices = nodeServices;
        }

Call NodeServices as shown below

public async Task<IActionResult> About()
{
  var msg = await nodeServices.InvokeAsync<string>(“./hello.js”,1);
  return View();
}

Add following code to js file

module.exports = function (callback)
{
  var message = ‘Hello world’;
  callback(null, message);
};
Tony Ngo
  • 19,166
  • 4
  • 38
  • 60