5

我只想在beginForm()

如果我尝试使用
Html.BeginForm(null, null, FormMethod.Post, new {@id="Id"})

那么生成的 Html 是
<form action="/newquestion/payment/b9f88f80-f31f-4144-9066-55384c9f1cfc" ... >

我不知道该操作 url 是如何生成的,所以我尝试
Html.BeginForm(new {@id="Id"})
了,但是操作 url 看起来像这样
<form action="/newquestion/payment/Id... >

在这两种情况下,操作 url 都不是它应该的,并且它没有达到控制器的 post 操作。

4

3 回答 3

5

当您尝试生成路由时,例如 with BeginForm,MVC 将尽最大努力包含您可能需要的内容。

如果您在domain.com/Home/Index/b9f88f80-f31f-4144-9066-55384c9f1cfc并且使用您提供的代码,那么表单将在找到它们时使用操作、控制器和路由值生成。

controller / action / id
/Home      / Index  / b9f88f80-f31f-4144-9066-55384c9f1cfc

解决此问题的一种方法是强制id为空(例如空字符串)。

观察:

@using (Html.BeginForm(null, null, new { @id = string.Empty },
    FormMethod.Post, new { @id = "Id" }))
{

}

new { @id = string.Empty }是表示路由值的匿名对象。

于 2013-11-26T03:31:45.133 回答
1

将@id 更改为id。

 @using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "MyForm1" }))    
    {
        <input id="submit" type="submit" value="Search" />
    }
于 2013-11-26T02:48:32.710 回答
1

由于在System.Web.Mvc.Html (在System.Web.Mvc.dll中),开始表单的定义如下:-

BeginForm(this HtmlHelper htmlHelper, string actionName, string
controllerName, object routeValues, FormMethod method, object htmlAttributes)

像这样的意思: Html.BeginForm(string actionName, string controllerName, object routeValues, FormMethod method, object htmlAttributes)

所以,它在 MVC 4 中对我有用

@using (Html.BeginForm(null, null, new { @id = string.Empty }, FormMethod.Post,
    new { @id = "trainingForm" }))
{
    <input id="TRAINER_LIST" name="TRAINER_LIST" type="hidden" value="">
    <input type="submit" value="Create" id="btnSubmit" />
}
于 2015-09-06T15:01:15.273 回答