Programming/.NET

1. Model 이용하기

 

public class Employee
{
    public string Name;
    public int Age;
    public string Department;
    public DateTime Doe;
}

▶ Model

 

public ActionResult Index()
{
    Employee em = new Employee() { Name = "홍길동"Department = "기획팀"Age = 33, Doe = new DateTime(2016, 12, 05) };

    return View(em);
}

▶ Controller

 

@model test.Models.Employee

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        <table style="border:solidborder-width:1px;">
            <thead>
                <tr><th>항목</th><th></th></tr>
            </thead>
            <tbody>
                <tr><td>이름</td><td>@Model.Name</td></tr>
                <tr><td>나이</td><td>@Model.Age</td></tr>
                <tr><td>부서</td><td>@Model.Department</td></tr>
            </tbody>
        </table>
    </div>
</body>
</html>
▶ View

 

2. ViewBag 이용하기

 

public ActionResult Index()
{
    Employee em = new Employee() { Name = "홍길동"Department = "기획팀"Age = 33, Doe = new DateTime(2016, 12, 05) };

    ViewBag.Title = "사원테이블";
    ViewBag.Width = "5px";
    ViewBag.Resignation = false;
    ViewBag.IsMarriage = true;
    ViewBag.IsExecutive = null;

    return View(em);
}

▶ Controller

 

@model test.Models.Employee

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        퇴사 : <input type="checkbox" checked="@ViewBag.Resignation" /><br />
        결혼 : <input type="checkbox" checked="@ViewBag.IsMarriage" /><br />
        임원 : <input type="checkbox" checked="@ViewBag.IsExecutive" />
        <table style="border:solid; border-width:@ViewBag.Width;">
            <caption>@ViewBag.Title</caption>
            <thead>
                <tr><th>항목</th><th></th></tr>
            </thead>
            <tbody>
                <tr><td>이름</td><td>@Model.Name</td></tr>
                <tr><td>나이</td><td>@Model.Age</td></tr>
                <tr><td>부서</td><td>@Model.Department</td></tr>
            </tbody>
        </table>
    </div>
</body>
</html>
▶ View

 

참고로 checkbox처리의 경우 true만 checked 처리되고 false나 null은 아예 속성처리가 무시됩니다.

 

3. 구문

 

<span>

    @if ((bool)ViewBag.Resignation)
    {
        @: 이직
    }
    else {
        @: 재직
    }
</span>

▶ View

 

Razor 구문은 @문자로 시작합니다. 참고로 문자열은 기본적으로 C#문으로 인식하는데 구문안에서 단순한 문자열을 출력할때는 @: 기호를 사용합니다.

 

public ActionResult Index()
{
    Employee[] em = {
        new Employee() { Name = "홍길동"Department = "기획팀"Age = 33, Doe = new DateTime(2016, 12, 05) },
        new Employee() { Name = "홍길순"Department = "경영팀"Age = 25, Doe = new DateTime(2004, 9, 8) },
        new Employee() { Name = "홍길남"Department = "영업팀"Age = 40, Doe = new DateTime(2012, 4, 17) }
    };

    return View(em);
}

▶ Controller

 

@model test.Models.Employee[]

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        <table style="border:solid; border-width:@ViewBag.Width;">
            <caption>@ViewBag.Title</caption>
            <thead>
                <tr><th>항목</th><th></th></tr>
            </thead>
            <tbody>
                @foreach (test.Models.Employee em in Model) {
                    <tr><td>이름</td><td>@em.Name</td></tr>
                    <tr><td>나이</td><td>@em.Age</td></tr>
                    <tr><td>부서</td><td>@em.Department</td></tr>
                }
            </tbody>
        </table>
    </div>
</body>
</html>
▶ View

 

배열과 같은 모델의 데이터를 열거할때는 @model 에서 모델형식을 Employee[] 처럼 배열로 지정해야 합니다.

 

4. Namespace

 

@using test.Models
@model test.Models.Employee[]

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        <table style="border:solid; border-width:@ViewBag.Width;">
            <caption>@ViewBag.Title</caption>
            <thead>
                <tr><th>항목</th><th></th></tr>
            </thead>
            <tbody>
                @foreach (Employee em in Model) {
                    <tr><td>이름</td><td>@em.Name</td></tr>
                    <tr><td>나이</td><td>@em.Age</td></tr>
                    <tr><td>부서</td><td>@em.Department</td></tr>
                }
            </tbody>
        </table>
    </div>
</body>
</html>
▶View

 

Namespace도 일반 C#구문과 마찬가지로 using을 사용합니다. 다만 Razor작성 규칙에 따라 @를 붙여야 합니다.

'Programming > .NET' 카테고리의 다른 글

Debug / Release  (0) 2019.01.15
[ASP.NET MVC] Razor  (0) 2019.01.08
[ASP.NET MVC] URL 다루기  (0) 2018.12.18
[C#] 비트(bit) 연산자  (0) 2018.11.20
[ASP.NET MVC] 액션 메서드(Action Method)  (0) 2018.11.13
[C#] Thread  (0) 2018.11.06
0 0