HTML.BeginForm 始终将 Post 发送到索引操作
HTML.BeginForm sends Post always to index action
我正在尝试通过 html beginForm post 表单,但调用的方法始终是 [httpPost] 索引,而不是 post(搜索)中指定的方法。
我可能做错了什么?
这是我的表格:
@using (Html.BeginForm("Search", "MyController", FormMethod.Post))
{
<p>
Postal Code: @Html.TextBoxFor(m => m.PostalCode) <br />
City: @Html.TextBoxFor(m => m.PostalCodeCity ) <br />
Address: @Html.TextBoxFor(m => m.Address) <br />
<input type="submit" value="submit" />
</p>
}
我的模特:
public class MyModel
{
public string PostalCode { get; set; }
public string PostalCodeCity { get; set; }
public string Address { get; set; }
}
我的路线:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Ignore("{resource}.axd/{*pathInfo}");
//Routing for ASP.NET MVC Controllers
routes.MapRoute(
name: "ControllersRoute",
url: "mvc/{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional });
//Routing for Web Api Controller
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional });
我的控制器方法
public class MyController : Controller
{
/// <summary>
/// Gets or sets the message.
/// </summary>
[Category("String Properties")]
public string Message { get; set; }
/// <summary>
/// This is the default Action.
/// </summary>
public ActionResult Index()
{
MyModel model = new MyModel();
return View("Default", model);
}
[HttpPost]
public ActionResult Index(MyModel model)
{
//Refresh model attending session variables
return View("Default", model);
}
[HttpPost]
public ActionResult Search(MyModel model)
{
//model work
return View("Default", model);
}
}
更新
@markpsmith 建议我检查表单操作,结果是错误的。
它是 action="order-calendar" 而不是我将其更改为 action="order-calendar/search" 并且调用了操作搜索。
这是路由问题吗?
BeginForm 助手使用路由引擎到达控制器的 "search" 操作。该助手在 RouteTable class 中使用名为 GetVirtualPath() 的方法(类似于 RouteTable.Route.GetVirtualPath())。
如果您对 post 操作使用相同的页面,您可以简单地使用
@using (Html.BeginForm())
没有任何参数。您可以使用与您的路由设置兼容的 URL 来实现 "search" 操作吗?
我看到你已经标记了 Sitefinity,如果是这样你应该试试:
@使用Telerik.Sitefinity.UI.MVC;
@使用 Telerik.Sitefinity.Frontend.Mvc.Helpers
@using(Html.BeginFormSitefinity()){}
我正在尝试通过 html beginForm post 表单,但调用的方法始终是 [httpPost] 索引,而不是 post(搜索)中指定的方法。
我可能做错了什么?
这是我的表格:
@using (Html.BeginForm("Search", "MyController", FormMethod.Post))
{
<p>
Postal Code: @Html.TextBoxFor(m => m.PostalCode) <br />
City: @Html.TextBoxFor(m => m.PostalCodeCity ) <br />
Address: @Html.TextBoxFor(m => m.Address) <br />
<input type="submit" value="submit" />
</p>
}
我的模特:
public class MyModel
{
public string PostalCode { get; set; }
public string PostalCodeCity { get; set; }
public string Address { get; set; }
}
我的路线:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Ignore("{resource}.axd/{*pathInfo}");
//Routing for ASP.NET MVC Controllers
routes.MapRoute(
name: "ControllersRoute",
url: "mvc/{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional });
//Routing for Web Api Controller
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional });
我的控制器方法
public class MyController : Controller
{
/// <summary>
/// Gets or sets the message.
/// </summary>
[Category("String Properties")]
public string Message { get; set; }
/// <summary>
/// This is the default Action.
/// </summary>
public ActionResult Index()
{
MyModel model = new MyModel();
return View("Default", model);
}
[HttpPost]
public ActionResult Index(MyModel model)
{
//Refresh model attending session variables
return View("Default", model);
}
[HttpPost]
public ActionResult Search(MyModel model)
{
//model work
return View("Default", model);
}
}
更新
@markpsmith 建议我检查表单操作,结果是错误的。 它是 action="order-calendar" 而不是我将其更改为 action="order-calendar/search" 并且调用了操作搜索。
这是路由问题吗?
BeginForm 助手使用路由引擎到达控制器的 "search" 操作。该助手在 RouteTable class 中使用名为 GetVirtualPath() 的方法(类似于 RouteTable.Route.GetVirtualPath())。 如果您对 post 操作使用相同的页面,您可以简单地使用
@using (Html.BeginForm())
没有任何参数。您可以使用与您的路由设置兼容的 URL 来实现 "search" 操作吗?
我看到你已经标记了 Sitefinity,如果是这样你应该试试:
@使用Telerik.Sitefinity.UI.MVC; @使用 Telerik.Sitefinity.Frontend.Mvc.Helpers
@using(Html.BeginFormSitefinity()){}