BeginForm渲染一个空白的action方法

发布于 2024-11-10 17:08:25 字数 2043 浏览 1 评论 0原文

问题是每当我在 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 });
    }
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

靖瑶 2024-11-17 17:08:25

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

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

PM>安装包RouteDebugger

让我知道它如何为您工作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文