2. Views 폴더의 FormValidationDemo 폴더에 HelperMethod.cshtml 뷰 페이지를 생성 후 다음과 같이 코드를 작성한다. HelperMethod 뷰 페이지 작성 시 <form /> 태그 대신 Html.BeginForm() 메서드를 사용하는 방식으로 폼을 구성할 수 있다. 단순히 Html.BeginForm() 메서드를 사용하면 기본 폼 태그가 만들어진다. BeginForm 메서드는 여러 개로 다중 정의(overload)되어 있다. BeginForm 메서드의 매개 변수를 어떻게 지정하느냐에 따라서 여러 가지 다른 형태로 표현이 가능한데, 이번 실습에서는 그중 하나를 사용해보자.

    ▼  /Views/FormValidationDemo/HelperMethod.cshtml

    @{
      Layout = null;
    }
    
    <!DOCTYPE html>
    <html>
    <head>
      <title>헬퍼 메서드</title>
      <script>
          function CheckForm() {
              var name = document.getElementById(“txtName”);
              var content = document.getElementById(“txtContent”);
              if (name.value.length < 1 || content.value.length < 1) {
                  window.alert(“이름과 내용을 입력하시오.”);
                  name.focus();
                  name.select();
                  return false;
              }
              return true;
          }
      </script>
    </head>
    <body>
    <h2>헬퍼 메서드로 폼 구성</h2>
    
    @@using (Html.BeginForm())@
    @using (Html.BeginForm(“HelperMethod”, “FormValidationDemo”, new { },
                      FormMethod.Post, false, new
                      {
                          @class = “FormStyle”,
                          data_ng_test = “test”,
                          onsubmit = “return CheckForm();”
                      }))
    {
      <text>이름: </text> @Html.TextBox(“txtName”)<br />
      <text>내용: </text> @Html.TextBox(“txtContent”)<br />
      <input type=“submit” value=“입력” />
    }
    
    @if (ViewBag.ResultString != null)
    {
      @ViewBag.ResultString
    }
    </body>
    </html>
    

    신간 소식 구독하기
    뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.