Asp.net mvc 4 为什么提交按钮会';调用控制器功能?
我在调用控制器函数时遇到了一个小问题。奇怪的是,每隔一个提交按钮都可以正常工作。但这个问题我现在还不能解决。 我将向您展示两个带有提交按钮的表单,因为只有一个可以正常工作 控制器:Asp.net mvc 4 为什么提交按钮会';调用控制器功能?,asp.net-mvc-4,razor,Asp.net Mvc 4,Razor,我在调用控制器函数时遇到了一个小问题。奇怪的是,每隔一个提交按钮都可以正常工作。但这个问题我现在还不能解决。 我将向您展示两个带有提交按钮的表单,因为只有一个可以正常工作 控制器: public class MyController : Controller { public ActionResult MethodOne() { ... return RedirectToAction("index"); }
public class MyController : Controller
{
public ActionResult MethodOne()
{
...
return RedirectToAction("index");
}
public ActionResult MethodTwo()
{
...
return RedirectToAction("index");
}
}
以及以下观点:
//This one works fine!!
@using (Html.BeginForm("MethodOne", "My", FormMethod.Post))
{
<input id="Some-cool-id" type="submit" value="Add!" />
}
//This one doesn't work?!
@using (Html.BeginForm("MethodTwo", "My", FormMethod.Post))
{
<input id="some-cool-id2" type="submit" value="Delete"!" />
}
我正在搜索不好的内容,但最后,我需要帮助,谢谢。在方法之前添加属性
[HttpPost]
。试试这个
@using (Html.BeginForm("MethodTwo", "Test", FormMethod.Post))
{
<input type="submit" value="asd" />
}
@using (Html.BeginForm("MethodOne", "Test", FormMethod.Post))
{
<input type="submit" value="poll" />
}
我已尝试删除缓存并重新启动Visual studio。如果确实存在问题,则在第二个表单中,我将在提交按钮之前创建webgrid。然而,只向他展示(还没有使用数据)呃<代码>“删除”!“——有多余的
”
?(除非这是复制粘贴错误)这只是复制粘贴错误。在项目中,这是正确的。很抱歉,无论如何,这是一个很好的观察!不幸的是,没有帮助(路由规则是什么?问题不在那里。仍在下降。@Ademar两个表单在同一页中?是在同一页中(视图)…真的吗?然后我尝试创建新控制器并复制这些函数。@Ademar我只是更新了我的代码。这是一个示例,它工作得很好。我已经尝试创建新控制器并将所有操作复制到那里。你知道吗?它开始工作得很好。我还不知道这是怎么可能的。所以谢谢,发现错误主要是谢谢:)
@using (Html.BeginForm("MethodTwo", "Test", FormMethod.Post))
{
<input type="submit" value="asd" />
}
@using (Html.BeginForm("MethodOne", "Test", FormMethod.Post))
{
<input type="submit" value="poll" />
}
public class TestController : Controller
{
public ActionResult MethodOne()
{
return RedirectToAction("CustomerInfo");
}
public ActionResult MethodTwo()
{
return RedirectToAction("CustomerInfo");
}
[HttpGet]
public ActionResult CustomerInfo()
{
ViewBag.CustomerNameID = new SelectList(List, "CustomerId", "customerName");
ViewBag.RegisterItems = GetAllRegisterData();
ViewData["SampleList"] = GetAllRegisterData();
return View();
}
}