提交相同操作的表单,带有 id
我正在尝试做一些看起来非常简单的事情:创建一个表单,使用 id 提交到请求的同一 URL。
如果我不关心 id,我可以这样做:
<% using(Html.Form()) { %>
<!-- stuff -->
<% } %>
但由于我想要 id,所以我必须使用不同的重载。
我想要这样的东西:
<% using(Html.Form(some, args, new {id="myAwesomeForm"})) { %>
<!-- stuff -->
<% } %>
我不能只对操作和控制器进行硬编码,因为该表单在几个不同的地方使用。有时 URL 会有参数(/items/edit/1
,有时则不会 /items/create
),
必须有一些非常简单的方法来执行此操作当我看到它时,会让我觉得自己像个白痴。那么,它是什么?
说明:我的意思是 HTML 元素上的 id,如 ;
I'm trying to do something that seems very simple: create a form that submits to same URL it was requested from, with an id.
If I didn't care about the id, I could do:
<% using(Html.Form()) { %>
<!-- stuff -->
<% } %>
But since I want the id, I have to use a different overload.
I would like something along the lines of:
<% using(Html.Form(some, args, new {id="myAwesomeForm"})) { %>
<!-- stuff -->
<% } %>
I can't just hardcode the action and controller because the form is used in a couple of different places. Sometimes the URL will have parameters (/items/edit/1
, and other times it will not /items/create
)
There must be some incredibly simple way of doing this that is going to make me feel like an idiot when I see it. So, what is it?
Clarification: I mean an id on the HTML element, as in <form action="/my/action[/possible arguments]" id="myAwesomeForm"></form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对操作和控制器使用 null;它们将从当前操作和控制器中填充。
Use null for the action and controller; they will be filled in from the current action and controller.
只需使用第一个重载(routeValues As Object),
它将采用当前的区域名称、控制器名称和操作名称参数。 Post 是默认的表单方法。
Just use the first overload (routeValues As Object)
It will assume the current, Area Name, Controller Name and Action Name parameters. Post is the default form method.