使用 HTML onclick 按钮调用 C# 函数
Call a C# function using HTML onclick button
我正在使用 asp.net(C#)。我创建了一个 razor 页面并希望使用 html 按钮单击在 C# 中调用一个函数。该函数执行类似于稳定婚姻问题的匹配算法。例如,当管理员点击按钮 "Button1" 时,必须调用 C# 中的 matching() 函数,该函数应执行函数内的语句,最后 return 匹配列表。我在表格中看到了一些通用的答案,但我需要更具体的答案。提前致谢
如前所述,这是一种匹配算法 - 执行具有单侧偏好的双侧匹配。
我已经尝试过 html "Button1_Click" 解决方案。
这是非常通用的,不符合我的代码。
这是我目前所做的:
html代码:
<form id="form1" runat="server">
<button type="submit" id="cmdAction" text="Button1" runat="server">
Button1
</button>
</form>
C#代码:
public ActionResult OnGet()
{
if (NTUser.Role != Role.Admin)
{return RedirectToPage("/Denied");}
matching();
return RedirectToPage();
}
public void matching()
{//body}
我在 html 侧使用的按钮是 "Match and Show the result"
我希望输出是一个类似于 excel sheet 的列表,如果需要的话可以是 edited/deleted。
编辑:
如果有人知道如何使用枚举 and/or bool
我也会很感激
无论你想做什么都可以用 ajax 来完成,因为简单 button_Click 将无法使用剃须刀并且 MVC.So 进行 ajax 调用及以上我会建议您使用 Ajax.BeginForm,这样更容易。
https://www.c-sharpcorner.com/UploadFile/0c1bb2/ajax-beginform-in-Asp-Net-mvc-5/
看看他的文章。Ajax beginform 基本上允许您在 ajax 响应 returns 时访问成功方法,这样您就可以在 OnSuccess 中访问从控制器返回的结果方法。
您可以调用一个方法来检查角色并重定向到另一个操作,或者只调用一个私有方法。
public ActionResult OnGet()
{
if (NTUser.Role != Role.Admin)
{
return RedirectToPage("/Denied");
}
else
{
return RedirectToAction(Matching);
}
...other if with return
}
public ActionResult Matching()
{
//dosomething
}
查看
如果你需要post就使用它
@using (Html.BeginForm("OnGet", "YourControllerName", FormMethod.Post))
{
<button type="submit" id="cmdAction" runat="server">
Match and Show the result
</button>
}
如果你需要就得到
@Html.ActionLink("Match and Show the result", "OnGet", "YourControllerName") //without params
不要忘记添加属性 [HttpPost]
或 [HttpGet]
(获取代码只是为了更好的可读性)
试试这个:
<a href="@Url.Action("OnGet", "ControllerName"> Display Text</a>
有很多方法可以做到这一点,既然你提到了 razor pages,所以我建议你完成这个 guide
只需使用像这样的剃刀语法
@using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post)) {
<input type="submit" value="Match" />
}
我正在使用 asp.net(C#)。我创建了一个 razor 页面并希望使用 html 按钮单击在 C# 中调用一个函数。该函数执行类似于稳定婚姻问题的匹配算法。例如,当管理员点击按钮 "Button1" 时,必须调用 C# 中的 matching() 函数,该函数应执行函数内的语句,最后 return 匹配列表。我在表格中看到了一些通用的答案,但我需要更具体的答案。提前致谢
如前所述,这是一种匹配算法 - 执行具有单侧偏好的双侧匹配。 我已经尝试过 html "Button1_Click" 解决方案。 这是非常通用的,不符合我的代码。
这是我目前所做的:
html代码:
<form id="form1" runat="server">
<button type="submit" id="cmdAction" text="Button1" runat="server">
Button1
</button>
</form>
C#代码:
public ActionResult OnGet()
{
if (NTUser.Role != Role.Admin)
{return RedirectToPage("/Denied");}
matching();
return RedirectToPage();
}
public void matching()
{//body}
我在 html 侧使用的按钮是 "Match and Show the result"
我希望输出是一个类似于 excel sheet 的列表,如果需要的话可以是 edited/deleted。
编辑: 如果有人知道如何使用枚举 and/or bool
我也会很感激无论你想做什么都可以用 ajax 来完成,因为简单 button_Click 将无法使用剃须刀并且 MVC.So 进行 ajax 调用及以上我会建议您使用 Ajax.BeginForm,这样更容易。
https://www.c-sharpcorner.com/UploadFile/0c1bb2/ajax-beginform-in-Asp-Net-mvc-5/
看看他的文章。Ajax beginform 基本上允许您在 ajax 响应 returns 时访问成功方法,这样您就可以在 OnSuccess 中访问从控制器返回的结果方法。
您可以调用一个方法来检查角色并重定向到另一个操作,或者只调用一个私有方法。
public ActionResult OnGet()
{
if (NTUser.Role != Role.Admin)
{
return RedirectToPage("/Denied");
}
else
{
return RedirectToAction(Matching);
}
...other if with return
}
public ActionResult Matching()
{
//dosomething
}
查看
如果你需要post就使用它
@using (Html.BeginForm("OnGet", "YourControllerName", FormMethod.Post))
{
<button type="submit" id="cmdAction" runat="server">
Match and Show the result
</button>
}
如果你需要就得到
@Html.ActionLink("Match and Show the result", "OnGet", "YourControllerName") //without params
不要忘记添加属性 [HttpPost]
或 [HttpGet]
(获取代码只是为了更好的可读性)
试试这个:
<a href="@Url.Action("OnGet", "ControllerName"> Display Text</a>
有很多方法可以做到这一点,既然你提到了 razor pages,所以我建议你完成这个 guide
只需使用像这样的剃刀语法
@using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post)) {
<input type="submit" value="Match" />
}