Reputation: 65
I am working with asp.net(C#). I created a razor page and want to call a function in C# using the html button click. The function performs a matching algorithm similar to the stable marriage problem. For example, when the Admin clicks on button "Button1", the function matching() in C# has to be called, which should execute the statements within the function and return the matched list in the end. I have seen some generic answers in the form but I need something more specific. Thanks in advance
As mentioned, this is a matching algorithm- to perform a two-sided matching with one sided preference. I have already tried the html "Button1_Click" solution. this is very generic and doesn't go with my code.
this is what I have done so far:
html code:
<form id="form1" runat="server">
<button type="submit" id="cmdAction" text="Button1" runat="server">
Button1
</button>
</form>
C# code:
public ActionResult OnGet()
{
if (NTUser.Role != Role.Admin)
{return RedirectToPage("/Denied");}
matching();
return RedirectToPage();
}
public void matching()
{//body}
The button I am using on the html side is "Match and Show the result"
I expect the output to be a list like an excel sheet that can be edited/deleted if need be.
Edit: I would also appreciate if someone has an idea how to do it with an enum and/or bool
Upvotes: 3
Views: 8611
Reputation: 1101
You can call one method where you check role and redirect on another action or just call a private method.
public ActionResult OnGet()
{
if (NTUser.Role != Role.Admin)
{
return RedirectToPage("/Denied");
}
else
{
return RedirectToAction(Matching);
}
...other if with return
}
public ActionResult Matching()
{
//dosomething
}
view
if you need post then use it
@using (Html.BeginForm("OnGet", "YourControllerName", FormMethod.Post))
{
<button type="submit" id="cmdAction" runat="server">
Match and Show the result
</button>
}
if you need just get
@Html.ActionLink("Match and Show the result", "OnGet", "YourControllerName") //without params
dont forget add attribute [HttpPost]
or [HttpGet]
(get just for better readability code)
Upvotes: 1
Reputation: 1424
There are a lot of ways to do this, since you'ver mentioned razor pages so i'll recommend go through this guide
Simply use razor syntax like this
@using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post)) {
<input type="submit" value="Match" />
}
Upvotes: 0
Reputation: 132
Try this:
<a href="@Url.Action("OnGet", "ControllerName"> Display Text</a>
Upvotes: 0