how to call a controller GET method in a view? (.NET 6.0)
How to display the result after clicking, for example, on a button?
Controller Method:
public string GetString()
{
string time = "Operation time: " + _lastExecutionTime;
return time;
}
2
Answers
I think what you really want to achieve is to get your Action Method working on the View. No need to write it in the controller. You will need to do something like this on your View.
And then you will call it in your HTML(DOM) block like this
All these are on the View.
But if you must call the Action Method in the Controller, you need the Action Method to return an ActionResult – which may be a View or Redirect. but not to return a literal value.
You can do that by the following code on your View
And then in your Process Controller your action method will be like
Also you can call a Get Action method by just changing the form method to GET.
You need to know that the GET is mostly used when doing searches.
Most Times you will be calling the Action Methods, you also need to know that your action method in the controller will contain arguments which will be an object of Entity Model. That is when you are using the strongly typed views.
The HTML code above can be written in C# like this
OR
If you are in the View of myData Action Method in Process controller.
Or you can use ajax to call the backend API:
Result:click button