Reputation:
I am new to asp.net core and Razor, i am trying to call a function from cshtml page to his inner cs page:
<button onclick="@{Model.GetUserInfo(1);};" type="submit">Search</button>
cshtsml.cs
public UserInfo GetUserInfo(int userId)
{
using (var client = new HttpClient())
{
var response = client.GetAsync($"localhost:44322/api/User/user/{userId}{userId}");
var body = response.Result.Content.ReadAsAsync<UserInfo>().Result;
return body;
}
}
I would like to get the information back from the api and display the information that i received.
With this code if i put { } around the Model.GetUserInfo(1); it doesn't display the button, without the { } it just doesn't compile.
Any of you can help me with it? thanks.
Upvotes: 0
Views: 4500
Reputation: 163
Step 1 - You can write a javascript function which will send an ajax request to your controller method
Step 2 - Return the data you want from this method
Assuming your controller name is Home, you can do something like this-
<button onclick="GetData()" type="submit">Search</button>
function GetData() {
$.get("/Home/GetUserInfo", function (data) {
// Here put what do you want from received data
});
}
Upvotes: 1