3

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

4
  • For me, your code looks alright. Any exceptions? Commented Jun 25, 2019 at 5:44
  • Are you using razor syntex or plain html to call the Action on Controller? It will be more helpfull of you update your View's code here Commented Jun 25, 2019 at 6:16
  • Seems to me that you are confusing Web Forms (runat="server") with Razor Pages. They are two different, and incompatible, frameworks. If you are using Razor Pages and want to determine which button was clicked, use named handlers:learnrazorpages.com/razor-pages/… Commented Jun 25, 2019 at 13:19
  • I am not trying to find out which button was clicked. I want to call a C# function on the server side when a button is clicked on the client side. Commented Jun 26, 2019 at 7:02

3 Answers 3

1

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)

Sign up to request clarification or add additional context in comments.

2 Comments

This is on the C# side. But how will it be connected to the button on the html side?
Also, I have edited my question. So if you have any suggestions on that, they are most welcome
0

Try this:

<a href="@Url.Action("OnGet", "ControllerName"> Display Text</a>

Comments

0

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" />
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.