skip to Main Content

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


  1. 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.

    @functions{
       public string GetString()
        {
        string time = "Operation time: " + _lastExecutionTime;
        return time;
       }
    }
    

    And then you will call it in your HTML(DOM) block like this

    <div>
          <table>
                 <tr> <td> @GetString() </td></tr>
          </table>
    </div>
    

    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

    <form method="post" action="/Process/myData">
      <input type="text" name="fullname">
      <input type="submit">
    </form>
    

    And then in your Process Controller your action method will be like

    public ActionResult myData(){
      ...do something...
      return View();
    }
    

    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

     @using (Html.BeginForm("myData", "Process", FormMethod.Post, new { @class = "user", role = "form" })){
      @Html.Editor("fullname", new { htmlAttributes = new { @class = "form-control form-control-user", @required = "required", @Placeholder="fullname" } })
    <input type="submit"/>
                      }  
    

    OR

    @using (Html.BeginForm()){
       @Html.Editor("fullname", new { htmlAttributes = new { @class = "form-control form-control-user", @required = "required", @Placeholder="fullname" } })
    <input type="submit"/>}
    

    If you are in the View of myData Action Method in Process controller.

    Login or Signup to reply.
  2. Or you can use ajax to call the backend API:

     [Route("api/[controller]")]
        [ApiController]
        public class ValuesController : ControllerBase
        {
            public string GetString()
            {
                string time = "Operation time: " + DateTime.Now;
                return time;
               
            }
        }
    
    <input type="button" id="btnGet" />
    <div id="value"></div>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#btnGet").click(function () {
                $.ajax({
                    type: "Get",
                    url: "api/Values",
                    success: function (response) {
                        alert(response);
                        $("#value").html(response);
                    },
                    failure: function (response) {
                        alert(response.responseText);
                    },
                    error: function (response) {
                        alert(response.responseText);
                    }
                });
            });
        });
    </script>
    

    Result:click button

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search