programing

Html에 ID 속성을 추가하는 방법.asp.net mvc에서 양식()을 시작하시겠습니까?

lovejava 2023. 8. 2. 08:37

Html에 ID 속성을 추가하는 방법.asp.net mvc에서 양식()을 시작하시겠습니까?

jquery를 사용하여 양식의 유효성을 확인하고 싶지만 양식에 다음이 없습니다.ID현재 속성은 어떻게 asp.net mvc의 양식에 추가합니까?이걸 사용하고 있어요...

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

그리고 내 jquery validator 플러그인은 이것을 가져갑니다.

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

이제 저는 ID를 주고 싶습니다.signupform어떤 제안이라도...

그러면 ID가 추가됩니다.

ASP.NET MVC 5 이하:

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

ASP.NET Core: 양식의 태그 도우미를 사용하여 ID를 설정하는 데 사용되는 홀수 구문을 피할 수 있습니다.

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

프로젝트에 코드를 추가해서 더 편리합니다.

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:

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

시스템에서.웹.MVC.Html(시스템).Web.Mvc.dll ) 시작 양식은 다음과 같이 정의됩니다. - 세부 정보

시작 양식(이 HtmlHelper htmlHelper, string actionName, string)
controllerName, objectrouteValues, FormMethod 메서드, object htmlAttributes)

다음과 같이 사용해야 함을 의미:

Html.BeginForm(string actionName, string controllerName, 객체 경로값, FormMethod 메서드, 객체 htmlAttributes)

그래서 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" />
}

조금 늦었을 수도 있지만 제 경우에는 두 번째 익명의 물건에 아이디를 넣어야 했습니다.첫 번째는 경로 값, 즉 반환 Url에 대한 것이기 때문입니다.

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

이것이 누군가에게 도움이 되기를 바랍니다 :)

언급URL : https://stackoverflow.com/questions/2854616/how-to-add-id-property-to-html-beginform-in-asp-net-mvc