Html.BeginForm()只用表单ID不产生正确的动作网址 [英] Html.BeginForm() with only form Id is not generating correct action url

查看:315
本文介绍了Html.BeginForm()只用表单ID不产生正确的动作网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只希望添加的formId在 beginForm()

I only want to add the formId in the beginForm()

如果我尝试使用结果
Html.BeginForm(NULL,NULL,FormMethod.Post,新{@ ID =ID})

然后生成的HTML的结果
<形式的行动=/ newquestion /支付/ b9f88f80-f31f-4144-9066-55384c9f1cfc...>

then the Html generated is
<form action="/newquestion/payment/b9f88f80-f31f-4144-9066-55384c9f1cfc" ... >

我不知道,行动网址是如何产生的,所以我试过了,结果
Html.BeginForm(新{@ ID =ID})结果
但随后的动作URL看起来是这样的结果
&LT;形式的行动=/ newquestion /支付/ ID ...&GT;

i don't know how that action url is generated so i tried,
Html.BeginForm(new {@id="Id"})
but then the action url looks like this
<form action="/newquestion/payment/Id... >

在这两种情况下的操作URL是不是应该和它不打控制器的POST操作。

In both the cases the action url is not what it should be and it is not hitting the post action of the controller.

推荐答案

当您试图生成路径,如 BeginForm ,MVC将尽最大努力包括您可能需要的东西。

When you are trying to generate a route, such as with BeginForm, MVC will do its best to include things that you might need.

如果您在 domain.com/Home/Index/b9f88f80-f31f-4144-9066-55384c9f1cfc 并使用您所提供的code不是形式将与动作,控制器和路由值发现他们产生的。

If you're at domain.com/Home/Index/b9f88f80-f31f-4144-9066-55384c9f1cfc and you use the code that you have provided than the form will be generated with the action, controller and route values as it finds them.

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

要解决这个问题的方法之一是强制的标识的是什么(如一个空字符串)。

One way to get around this is to force id to be nothing (such as an empty string).

注意:

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

}

新{@id =的String.Empty} 是一个匿名的对象,再presents路线值。

The new { @id = string.Empty } is an anonymous object that represents the route values.

这篇关于Html.BeginForm()只用表单ID不产生正确的动作网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆