0

In my DotNet Core application, I have a button set up with the some javascript placed in for my OnClick event. It looks like this:

<div>
    @(Html.DevExtreme().Button()
                             .Text("Press me")
                             .Type(ButtonType.Normal)
                             .Width(90)
                             .OnClick("notify")
    )
</div>

<script>
     function notify() {
        console.log("pressed");

       // ModifiedDuration.AdjustmentScreen_Netting.Csharp.RunBatch();
      //  var a = '<%=RunBatch()%>';
    }
</script>

The commented out lines are what I've tried to call my target method but neither have worked. The underlying method I want to call is this:

public void RunBatch()
{
    Console.WriteLine("Re-running batch");
    TestOutput print= new TestOutput ();
    print.TestMethod();
 }

Then, what TestMethod does:

public void ProcessAdjustedBatch()
{
    Console.WriteLine("I have been called from the datagrid!!!!");
}

So after I press the button, I would expect to see the following log messages:

  • Pressed
  • re-running batch
  • I have been called from the datagrid!!!!

But all that I see is Pressed in my dev log. How can I achieve my intended output?

N0xus
  • 2,674
  • 12
  • 65
  • 126
  • `function notify()` is evaluated at the client side, but I assume the `public void RunBatch()` is part of your backend. Therefore the browser can't use the function `RunBatch`. You could Expose an HTTP Interface for this method and invoe it over HTTP – Maximilian Ast Nov 14 '18 at 16:13
  • These are two dofferent parts. First one is writing to browser log and the second one is writing to Console. You are mixing up client and server. – Robert Nov 14 '18 at 16:14

1 Answers1

0

Console.WriteLine() will never shown in client's browser because it runs server-side and opened in server's console window. It is necessary to use Debug.WriteLine() if you want to see debug messages in immediate output window.

Also note that var a = '<%=RunBatch()%>' only executes inside ASPX pages, it cannot be executed in Razor which uses @{ ... } for code segment, and both debug methods are server-side methods.

If you want to show server-side debug message in browser's console with console.log(), you need to create a controller action that executes both methods which have string return type and then use AJAX inside notify() function which outputs the result in success part.

Here is a simple example:

Controller Action

public IActionResult GetMessages()
{
    var messages = new StringBuilder();

    messages.Append(RunBatch());
    messages.Append("\n"); // use newline to separate messages from different methods
    messages.Append(ProcessAdjustedBatch());

    return Json(messages.ToString(), JsonRequestBehavior.AllowGet);
}

Debug Methods

public static string RunBatch()
{
    string batch = "Re-running batch";
    Debug.WriteLine(batch);

    TestOutput print = new TestOutput();
    print.TestMethod();

    return batch;
}

public static string ProcessAdjustedBatch()
{
    return "I have been called from the datagrid!!!!";
}

View (script tag)

<script>
function notify() {
   console.log("pressed");

   $.ajax({
       type: 'GET',
       url: '@Url.Action("GetMessages", "ControllerName")',
       dataType: 'json',
       success: function (result) {
           console.log(result);
       }
   });
}
</script>

Now the console output should be like this:

pressed
Re-running batch
I have been called from the datagrid!!!!

Live example: .NET Fiddle

Related issues:

Where does Console.WriteLine go in ASP.NET?

Asp.net mvc Console.WriteLine to browser

How to use Console.WriteLine in ASP.Net MVC 3

Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61