본문 바로가기

Backend 개발/.NET

ASP.NET Form에서 Controller로 데이터 넘기기

반응형

ASP.NET으로 웹 개발 시 Form에서 Controller로 데이터를 넘기는 방식은 Spring과 매우 유사하다.

다음의 간단한 예제로 바로 이해할 수 있다.

[Route("[controller]/[action]")]
public class TestController : Controller
{
	..
    // form 화면 이동
    public IActionResult testForm()
        {
            return View();
        }

    [HttpPost]
    public IActionResult testForm(string userId, int age)
    {
        Console.WriteLine("userId = " + userId);
        Console.WriteLine("age = " + age);
        return Redirect("/test/testView");
    }
    
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Test</title>
</head>
<body>
   <form method="POST" action="/test/testForm">
    <input type="text" name="userId" />
    <input type="text" name="age" />
    <input type="submit" name="제출" />
   </form>
</body>
</html>

form 입력 화면
컨트롤러 데이터 전달받은 내용 콘솔 출력

 

728x90
반응형