我在C#中有一个简单的Web应用程序。
在Index.cshtml文件中,我想插入一个按钮,当我点击它时,调用我的Controller的一个方法。
所以我写了这段代码:
<div class="row" style="float:left;width:100%;height:100%;">
<div class="col-md-12" style="width:100%;height:100%;">
@using (Html.BeginForm("action", "IndexController"))
{
<input type="submit" value="Create" />
}
</div>
</div>
在我的控制器中,我构建了以下代码:
[HttpPost]
public ActionResult MyAction(string button)
{
return View("TestView");
}
但是,如果我试图单击按钮,应用程序就不会调用方法MyAction。
发布于 2019-08-23 07:55:44
ASP.NET MVC约定要求使用操作名称,而控制器名称减去" controller“,因此,由于操作方法的名称为"MyAction”,而控制器的名称为"IndexController",因此,请按如下方式更新调用:
@using (Html.BeginForm("MyAction", "Index"))
基于注释的更新:
要向操作方法发送文本以满足string button
参数,可以在表单中包括如下输入字段:
@using (Html.BeginForm("MyAction", "Index"))
{
<input type="text" name="button"/>
<input type="submit" value="Create" />
}
注意,文本字段的name
属性是“按钮”以匹配的string button
参数名称。
name
属性的输入字段都将为该参数提供值。https://stackoverflow.com/questions/57621831
复制相似问题