Unfortunately, it's not possible to achieve this because JavaScript code is only available after C# / Razor has been rendered. You can find more information about this on this thread.
My suggestion would be to use ajax to send a request to access the GetClientCaseType()
method.
Update:
If you want to send an ajax post request, follow these steps:
1. Add the following service in stratup.cs
services.AddAntiforgery(o => o.HeaderName = "XSRF-TOKEN");
2. Add the AntiForgeryToken to the current page
@Html.AntiForgeryToken();
3. Set the token to request header in ajax
beforeSend: function (xhr) {
xhr.setRequestHeader("XSRF-TOKEN",
$('input:hidden[name="__RequestVerificationToken"]').val());
},
Here's an example:
Create.cshtml:
@page
@model RazorApp.Pages.ClientCases.CreateModel
@Html.AntiForgeryToken();
<button id="btn" onclick="casetype()">Click</button>
@section scripts{
<script>
function casetype() {
var id = 1;
$.ajax({
url: 'Create?handler=GetUploadedFile',
type: "POST",
data: { id : id },
beforeSend: function (xhr) {
xhr.setRequestHeader("XSRF-TOKEN",
$('input:hidden[name="__RequestVerificationToken"]').val());
},
success: function (data) {
if (data != null) {
var vdata = data;
}
}
});
}
</script>
}
Create.cshtml.cs:
public class CreateModel : PageModel
{
public void OnGet()
{
}
public IActionResult OnPostGetUploadedFile(int id)
{
var result = "AAA";
return new JsonResult(result);
}
}
Please note that the page handler should be in the format OnPostXxx()
or OnGetXxx()
.
Also, make sure the url in ajax follows the format XXX?handler=Xxx
.
Result:
https://i.sstatic.net/jFVaW.gif