BeginForm渲染一个空白的动作方法

时间:2011-05-31 01:04:45

标签: .net asp.net-mvc asp.net-mvc-3

问题是每当我在BeginForm中对Action和Controller进行硬编码时,它就会产生一个空白的动作方法。

我很困惑。

从HomeController和Index操作方法调用了以下视图。

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

结果

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

从HomeController和Page动作方法调用了以下视图。

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

结果

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

路由

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

控制器操作

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

1 个答案:

答案 0 :(得分:1)

您应该尝试使用可以从NuGet下载的此工具调试路由。

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

  

PM&GT; Install-Package RouteDebugger

让我知道它对你有用。