본문 바로가기
Programming/C#

[C#] ASP.NET MVC에서 View로 데이터 보내는 여러가지 방법

by 코딩의성지 2022. 3. 11.

하이 ..!

 

지난번 포스팅에서 MVC의 기본동작 방식과 간단한 예제 코드를 포스팅 했었다.

https://devkingdom.tistory.com/337

 

[C#] ASP.NET에서 MVC 사용하여 프로젝트 구축하기

이직을 하면서 사용하는 기술 스택이 바꼈다. DB는 오라클에서 MSSQL 로 언어는 Java에서 C#으로...! 기본적으로 ASP.NET MVC에 대한 학습이 필요하여 오늘부터 조금씩 정리해 두려고 한다. 이미 mvc패턴

devkingdom.tistory.com

 

이전 포스팅에서 View로 데이터를 전달할때 파라미터로 모델객체를 넣어 전덜을 했었는데, 이렇게 전달을 하면 Model 하나만 전달이 가능해진다.

 

여러개의 데이터를 View에 전달하기 위해서는

 

ViewBag이나 ViewData를 이용하면된다. 이 방법을 이용하면 개수에 제한 없이 여러 데이터를 뿌려줄 수가 있다.

 

ViewBag

 

먼저 ViewBag을 이용한 예시이다.

 

        public ActionResult Find(int? Id) 
        {
           `````
           
            var club = clubActs.GetClub(1);

            ViewBag.Club = club;

     		`````
        }

먼저 동작하는 Action 메서드에 담고자하는 객체를 ViewBag 을 이용해서 담아주자.

 

그리고 View파일에서 아래와 같이 @ViewBag.~~~ 이런식으로 활용을 해주면 된다.

@using MvcApplication.Models
@model List<Member>
@{
    ViewBag.Title = "Find";
}

<!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>Member</title>
</head>
<body>
    <div>
        <p>
            @ViewBag.Club.Name 의 회원
        </p>
        <table>
            <thead>
                <tr>
                    <th>이름</th>
                    <th>나이</th>
                    <th>주소</th>
                </tr>
            </thead>
            <tbody>

               @foreach(var item in Model)
            {
                <tr>
                    <td>@item.Name</td>
                    <td>@item.Age</td>
                    <td>@item.Address</td>
                </tr>
            }
                </tbody>
        </table>
    </div>
</body>
</html>

결과는 아래와 같이 표현이 된다.

 

ViewData

ViewData를 이용하는 방법도 있다.

        public ActionResult Find(int? Id) 
        {
           `````
           
            var club = clubActs.GetClub(1);

            ViewData["Club"] = club;

     		`````
        }

Data를 넣는 방법은 배열에 데이터를 넣는 것처럼 하면된다.

 

다만 View에서는 별도로 변수를 만들어줘야한다.

아래 예시를 보자.

 

@using MvcApplication.Models
@model List<Member>
@{
    ViewBag.Title = "Find";
    var club = ViewData["Club"] as Club;
}

<!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>Member</title>
</head>
<body>
    <div>
        <p>
            @club.Name 의 회원
        </p>
        <table>
            <thead>
                <tr>
                    <th>이름</th>
                    <th>나이</th>
                    <th>주소</th>
                </tr>
            </thead>
            <tbody>

               @foreach(var item in Model)
            {
                <tr>
                    <td>@item.Name</td>
                    <td>@item.Age</td>
                    <td>@item.Address</td>
                </tr>
            }
                </tbody>
        </table>
    </div>
</body>
</html>

상단에 보면 ViewData를 가져오는 club변수를 따로 만들었고

그 변수를 그대로 활용하는 것을 볼 수 있다.

 

어렵지 않은 개념이니 여러분들도 한번 간단하게 실습해보시길 바란다.

 

끝.

반응형

'Programming > C#' 카테고리의 다른 글

[C#] ASP.NET에서 MVC 사용하여 프로젝트 구축하기  (5) 2022.03.10

댓글