【问题标题】:HTML.BeginForm sends Post always to index actionHTML.BeginForm 始终将 Post 发送到索引操作
【发布时间】:2015-08-28 13:28:44
【问题描述】:

我正在尝试通过 html beginForm 发布表单,但调用的方法始终是 [httpPost] 索引,而不是帖子中指定的方法(搜索)。

我做错了什么?

这是我的表格:

 @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" 并调用了操作 Search。

这是路线问题吗?

【问题讨论】:

  • 您是否调试过代码以确认它没有命中Search
  • 是的,我已经做到了:/ 我并没有意识到可能出了什么问题
  • 能否检查生成的 HTML 以确认表单操作为 Search
  • 可能与它是一个 WebApi 应用程序有关。需要默认路由吗?
  • 你的控制器被命名为MyController,因此它必须是@using (Html.BeginForm("Search", "My", FormMethod.Post))(第二个参数是"My",而不是"MyController") - 但其他cmets建议你有更多的问题不仅仅是这个

标签: asp.net-mvc sitefinity html.beginform


【解决方案1】:

我看到你已经标记了 Sitefinity,如果是这样,你应该试试:

@使用 Telerik.Sitefinity.UI.MVC; @使用 Telerik.Sitefinity.Frontend.Mvc.Helpers

@using(Html.BeginFormSitefinity()){}

【讨论】:

  • 谢谢先生!谢谢 正确!我现在有另一个问题,我会在另一个线程中发布。基本上点击后表单就消失了。
  • 您是否返回了有关您的帖子操作的视图?
  • 你好,我正在返回一个 RedirectToAction();但是元素在点击时立即消失。删除是在客户端。只有稍后回发完成。很奇怪的行为。
  • 如果没有看到代码的那部分,很难说发生了什么。您不想返回一个视图并显示该帖子是否成功?
  • 我刚刚在这里发布了疑问stackoverflow.com/questions/32370564/…让我知道你的想法:)
【解决方案2】:

BeginForm 助手使用路由引擎到达控制器的“搜索”动作。该助手使用 RouteTable 类中名为 GetVirtualPath() 的方法(类似于 RouteTable.Route.GetVirtualPath())。 如果您将同一页面用于发布操作,您可以简单地使用

@using (Html.BeginForm())

没有任何参数。您能否使用与您的路由设置兼容的 URL 来执行“搜索”操作?

【讨论】:

  • 你好 theLaw 我会试试看,但是你的意思是“你能使用与你的路由设置兼容的 URL 来达到搜索操作吗?”?例如,要从 javascript 访问 Search,我使用 URL.RouteURL 而不是 URL.Action,因为第二个返回了错误的路由。这是你的问题?
  • 尝试用浏览器去mvc/MyController/Search
  • 我收到“找不到资源。”。但如果我去 mvc/MyController/Index 就可以了。
  • 尝试使用@using (Html.BeginForm(ViewContext.RouteData.Values.GetRequiredString("Search"), ViewContext.RouteData.Values.GetRequiredString("mycontroller"))))
  • 并像这样在视图中强烈键入模型@model YOURPATH.MyModel
猜你喜欢
  • 2017-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-15
  • 1970-01-01
  • 2020-08-12
  • 2011-10-29
  • 1970-01-01
相关资源
最近更新 更多