jQuery 如何在 asp.net mvc 中向 Html.BeginForm() 添加 ID 属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2854616/
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-08-26 14:24:40  来源:igfitidea点击:

How to add ID property to Html.BeginForm() in asp.net mvc?

jqueryasp.net-mvcforms

提问by ACP

I want to validate my form using jquery but it doesn't have an IDproperty as of now how to add it to the form in asp.net mvc? I am using this...

我想使用 jquery 验证我的表单,但它现在没有ID属性,如何将它添加到 asp.net mvc 中的表单?我正在使用这个...

<% using (Html.BeginForm()) {%>

and my jquery validator plugin takes this,

我的 jquery 验证器插件采用了这个,

var validator = $("#signupform").validate({

Now i want to give id as signupform... Any suggestion...

现在我想给 id 作为signupform......任何建议......

回答by Jason Rowe

This should get the id added.

这应该添加 id。

ASP.NET MVC 5 and lower:

ASP.NET MVC 5 及更低版本:

<% using (Html.BeginForm(null, null, FormMethod.Post, new { id = "signupform" }))
   { } %>

ASP.NET Core: You can use tag helpers in formsto avoid the odd syntax for setting the id.

ASP.NET Core:您可以在表单中使用标签助手来避免设置 id 的奇怪语法。

<form asp-controller="Account" asp-action="Register" method="post" id="signupform" role="form"></form>

回答by ADM-IT

I've added some code to my project, so it's more convenient.

我在我的项目中添加了一些代码,所以更方便。

HtmlExtensions.cs:

HtmlExtensions.cs:

namespace System.Web.Mvc.Html
{
    public static class HtmlExtensions
    {
        public static MvcForm BeginForm(this HtmlHelper htmlHelper, string formId)
        {
            return htmlHelper.BeginForm(null, null, FormMethod.Post, new { id = formId });
        }

        public static MvcForm BeginForm(this HtmlHelper htmlHelper, string formId, FormMethod method)
        {
            return htmlHelper.BeginForm(null, null, method, new { id = formId });
        }
    }
}

MySignupForm.cshtml:

我的注册表格.cshtml:

@using (Html.BeginForm("signupform")) 
{
    @* Some fields *@
}

回答by Muhammad Ashikuzzaman

In System.Web.Mvc.Html( in System.Web.Mvc.dll) the begin form is defined like:- Details

System.Web.Mvc.Html(在System.Web.Mvc.dll 中)开始形式定义如下:-详细信息

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

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

Means you should use like this :

意味着你应该这样使用:

Html.BeginForm( string actionName, string controllerName,object routeValues, FormMethod method, object htmlAttributes)

Html.BeginForm( string actionName, string controllerName,object routeValues, FormMethod method, object htmlAttributes)

So, it worked in MVC 4

所以,它适用于 MVC 4

@using (Html.BeginForm(null, null, new { @id = string.Empty }, FormMethod.Post,
    new { @id = "signupform" }))
{
    <input id="TRAINER_LIST" name="TRAINER_LIST" type="hidden" value="">
    <input type="submit" value="Create" id="btnSubmit" />
}

回答by Daniaal

May be a bit late but in my case i had to put the id in the 2nd anonymous object. This is because the 1st one is for route values i.e the return Url.

可能有点晚了,但就我而言,我不得不将 id 放在第二个匿名对象中。这是因为第一个用于路由值,即返回 Url。

@using (Html.BeginForm("Login", "Account", new {  ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { id = "signupform", role = "form" }))

Hope this can help somebody :)

希望这可以帮助某人:)