Html.BeginForm with overload causes form action to

2019-09-09 20:04发布

Because I wanted to assign an id to a form element my code was @using (Html.BeginForm(null, null, FormMethod.Post, new { id = "indexform"}))

This resulted in markup <form action="/cgi-bin?action=Index&amp;controller=Home" id="indexform" method="post" style="position: relative;">

My HomeController ActionResult Index was completely bypassed.

Notice the '/cgi-bin'. This might be the problem and I think the culprit may be parameter 'FormMethod.Post' but I could not enter null there.

(BTW I worked around the id requirement by using jQuery var form = $("#btnShowProperty").closest("form");)

I did quite a bit of Googling on this with no luck.

2条回答
霸刀☆藐视天下
2楼-- · 2019-09-09 20:38

The problem is you're passing null for the action and controller in the first two arguments. Instead, pass the name of your action and controller and it will work.

Example, if your action is "Index" and your controller is "Home", then use:

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "indexform"})) {
    ...
}
查看更多
Summer. ? 凉城
3楼-- · 2019-09-09 20:49

The problem is mixing web pages and mvc especially using MapPageRoute AND MapRoute in Route.config. See full answer @Url.Action(“Action”, “Controller”) returns “/cgi-bin?action=Action&controller=Controller”

查看更多
登录 后发表回答