Search code examples
asp.net-mvcsitefinityhtml.beginform

HTML.BeginForm sends Post always to index action


I'm trying to post a form through html beginForm but the method called is always [httpPost] index instead of the method specified in the post (Search).

What could I be doing wrong?

Here's my form:

 @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>
    } 

My model:

public class MyModel
    {

        public string PostalCode { get; set; }

        public string PostalCodeCity { get; set; }

        public string Address { get; set; }

}

My Routes:

 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 });

My controller methods

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);
        }

    }

UPDATE

@markpsmith recommended me to check the form action and it was wrong. it was action="order-calendar" than I Change it to action="order-calendar/search" and the action Search was called.

Is this a Route Problem?


Solution

  • I see you have tagged Sitefinity, If so you should be try:

    @using Telerik.Sitefinity.UI.MVC; @using Telerik.Sitefinity.Frontend.Mvc.Helpers

    @using(Html.BeginFormSitefinity()){}