Pirzada
Pirzada

Reputation: 4713

BeginForm rendering a blank action method

The problem is whenever I hardcode Action and Controller in BeginForm it results in a blank action method.

I am confused.

Below view has been invoked from HomeController and Index action method.

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "first" }))
{}

Result

<form id="first" method="post" action="/Home"></form>

Below view has been invoked from HomeController and Page action method.

@using (Html.BeginForm("Edit", "Home", FormMethod.Post, new { id = "first" }))
{}

Result

<form id="first" method="post" action=""></form>

Routing

    routes.MapRoute(
        "RootUrlWithAction",
        "Home",
        new
            {
                controller = "Home", 
                action = "Index", 
                name = "home", 
                id = UrlParameter.Optional
            }
    );

    routes.MapRoute(
        "DynamicPages",
        "{name}/{id}",
        new
            {
                controller = "Home", 
                action = "Page", 
                id = UrlParameter.Optional
            }
    );

    routes.MapRoute(
        "EmptyUrl",
        "",
        new
            {
                controller = "Home", 
                action = "Index", 
                name = "home"
            }
    );

    routes.MapRoute(
        "Default",
        "{controller}/{action}/{name}/{id}",
        new
        {
            controller = "Home",
            action = "Index",
            name =  UrlParameter.Optional,
            id = UrlParameter.Optional
        } 
    );

Controller Actions

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Page(String name)
    {

        return View();
    }

    [HttpPost]
    public ActionResult Edit(Order orderVm)
    {
        var a = orderVm;
        string errorMessage = "hehehe";

        return Json(new Order { Message = errorMessage });
    }
}

Upvotes: 0

Views: 607

Answers (1)

Glenn Ferrie
Glenn Ferrie

Reputation: 10410

You should try to debug your routes with this tool that you can download from NuGet.

http://haacked.com/archive/2011/04/13/routedebugger-2.aspx

PM> Install-Package RouteDebugger

Let me know how it works for you.

Upvotes: 1

Related Questions