c# - 按下表单中的按钮时如何从控制器调用方法?
问题描述
主要问题是当我按下按钮时没有调用来自控制器的方法。我在 MyController 的索引视图中有这个:
@using (Html.BeginForm())
{
@* .... *@
<form action="DoSomethingAction" method="post">
<input type="text" id="IdString" name="IdString" placeholder="Enter id"/>
<input id="Submit1" name="Submit1" type="submit" value="DoSomething1" />
<input id="Submit2" name="Submit2" type="submit" value="DoSomething2" />
</form>
@* ..... *@
}
在 MyController 我有 DoSomethingAction 方法。我想调用此方法并在按下按钮时能够看到 IdString:DoSomething1 或 DoSomething2。这是来自控制器的 DoSomethingAction 方法:
public ActionResult DoSomethingAction()
{
string id= Request.Form["IdString"];
//string button=...
// ...
}
当我为 DoSomethingAction 方法设置断点时,我观察到它从未被调用过。我试图为这个方法添加 [Httppost] 属性,但它并没有解决我的问题。
我究竟做错了什么?之后,如何在 DoSomethingAction 方法中查看按下了哪个按钮?
解决方案
HTML 表单不能嵌套 HTML 表单。请阅读https://www.w3.org/TR/html5/forms.html#the-form-element
如果您需要多个表单,您可以将它们定义为兄弟姐妹。
推荐阅读
- applescript - AppleScript中如何判断输入属于哪种语言?
- python - Python BS4 抓取:AttributeError:“NavigableString”对象没有属性“文本”
- python - Django 模型比较不适用于浮点数据类型
- hibernate - 如果我不想在 Hibernate 中使用 @Generated 注释,还有什么其他选择?
- python - 如何在python中使用正则表达式捕获字符串并将其替换为所需的字符串
- android - Android 媒体播放器/框架种类
- php - PhpStorm:找不到类“PhpUnit\Framework\TestCase”(作曲家/自动加载)
- opencv - 使用opencv绘制形状
- fuzzing - Boofuzz 基于组值创建嵌套块
- google-apps-script - 从我的表单中保存谷歌表格中的信息