asp.net-mvc 如何使用 Html.BeginForm() 命名表单?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3683366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:28:14  来源:igfitidea点击:

How can I name a form with Html.BeginForm()?

asp.net-mvcrazor

提问by user426306

How do I give a name to a form in ASP.NET MVC using Html.BeginForm()? I want only the name, not the action or controller namebecause I want to post it through Javascript. I think it should be something like Html.BeginForm(id = "frm").

如何使用 ASP.NET MVC 为表单命名Html.BeginForm()我只想要名称,而不是操作或控制器名称,因为我想通过 Javascript 发布它。我认为它应该是这样的Html.BeginForm(id = "frm")

I tried the following:

我尝试了以下方法:

Html.BeginForm(null,null,new{id="frm",name="frm})

Html.BeginForm(new{@id="frm",@name="frm})

But the above code produces output like this:

但是上面的代码产生这样的输出:

<form action="/Main/Index/Id?name=Id" method="post">

回答by BritishDeveloper

Html.BeginForm(null, null, FormMethod.Get, new { name = "frm", id = "frm" })

You'll need to catch the form submit with your JavaScript

您需要使用 JavaScript 捕获表单提交

回答by Steven Archibald

Using MVC2, this is what worked for me:

使用 MVC2,这对我有用:

<% using (Html.BeginForm("Index", "Home", null, FormMethod.Post, new {@id = "myForm", @name = "myForm"})) {%>

回答by Brahmaiah Chowdary Murakonda

@HTML.BeginForm("Target-ViewName where you want to post the page","Controller Name",new {@name="Name of the Form", id="ID of the Form"}) 
{ //form here
}

回答by Paul Hadfield

Taken from this answer: How to pass in ID with Html.BeginForm()?

取自这个答案:How to pass in ID with Html.BeginForm()?

Can you not just do:

你不能只做:

Html.BeginForm(new {@id="Id", @name="Id"}); 

It can pay to search SO for answers as I've found many things I want to ask have already been encountered.

搜索 SO 寻找答案是值得的,因为我发现已经遇到了很多我想问的事情。