[.NET/ASP.NET] - [ASP.NET] ASP.NET MVC2 Site 만들기 (1)
[.NET/ASP.NET] - [ASP.NET] ASP.NET MVC2 Site 만들기 (2)
[.NET/ASP.NET] - [ASP.NET] ASP.NET MVC2 Site 만들기 (3)
[.NET/ASP.NET] - [ASP.NET] ASP.NET MVC2 Site 만들기 (4)
[.NET/ASP.NET] - [ASP.NET] ASP.NET MVC2 Site 만들기 (5)
[.NET/ASP.NET] - [ASP.NET] ASP.NET MVC2 Site 만들기 (6)
이번엔 Controller  와 View  를 만들어 보겠습니다.
그림을 그냥 쭉 훑어 보시기만 해도 될 듯합니다.
Solution 탐색기의 Context Menu 도 직관적으로 구성되어 있어서 생성하기도 쉽고요...
그럼 Controller  부터 시작해 보겠습니다.

Controller Folder 를 오른쪽 클릭합니다.


Add/Controller... 를 클릭하여 Controller 를 추가합니다.
새로 만들어질 Controller  의 이름을 입력합니다.
Action Method 를 생성하진 않을 겁니다.

만들어진 Controller 의 내용을 아래와 같이 index 함수와 info 함수를 입력합니다.

    1 Namespace MvcSampleApp

    2 

    3     Public Class CustomerController

    4         Inherits System.Web.Mvc.Controller

    5         Private repository As New AdventureWorksRepository()

    6 

    7         '

    8         ' GET: /Customer

    9 

   10         Function Index() As ActionResult

   11             Return View()

   12         End Function

   13 

   14         Public Function Index(ByVal page As Nullable(Of Integer)) As ActionResult

   15             Dim viewData = New CustomerViewData()

   16             Dim currentPage As Integer = If(page, 0)

   17             viewData.Customers = Me.repository.GetCustomers(currentPage, 10)

   18             viewData.NextPage = currentPage + 1

   19             viewData.PreviousPage = If((currentPage <= 0), 0, currentPage - 1)

   20             Return View(viewData)

   21         End Function

   22 

   23         Public Function Info(ByVal id As Integer) As ActionResult

   24             Dim customer = Me.repository.GetCustomerById(id)

   25             Return View(customer)

   26         End Function

   27 

   28     End Class

   29 

   30 End Namespace


Controller 하나 더 만들어 보죠.
이번엔 Action Method  도 만들어 보죠.


아래와 같은 함수가 기본적으로 만들어질텐데요...
기본적으로 만들어진 함수를 아래와 같이 수정합니다.

    1 Namespace MvcSampleApp

    2 

    3     Public Class AddressController

    4         Inherits System.Web.Mvc.Controller

    5         Private repository As New AdventureWorksRepository()

    6 

    7         '

    8         ' GET: /Address/Create/5

    9         Function Create(ByVal customerId As Integer) As ActionResult

   10             Dim addressViewData = New AddressViewData With

   11                 {

   12                     .CustomerId = customerId

   13                 }

   14             Return View(addressViewData)

   15         End Function

   16 

   17         '

   18         ' POST: /Address/Create

   19         <AcceptVerbs(HttpVerbs.Post)> _

   20         Function Create(ByVal customerId As Integer, ByVal collection As FormCollection) As ActionResult

   21             Try

   22                 Dim addressViewData = New AddressViewData()

   23                 UpdateModel(addressViewData)

   24                 Me.repository.AddAddress(addressViewData.Address, customerId)

   25                 Return RedirectToAction("Info", "Customer", New With {.id = customerId})

   26             Catch

   27                 Return View()

   28             End Try

   29         End Function

   30 

   31         '

   32         ' GET: /Address/Edit/5

   33         Public Function Edit(ByVal addressId As Integer, ByVal customerId As Integer) As ActionResult

   34             Dim addressViewData As New AddressViewData()

   35             addressViewData.Address = Me.repository.GetAddressById(addressId)

   36             addressViewData.CustomerId = customerId

   37             Return View(addressViewData)

   38         End Function

   39 

   40         '

   41         ' POST: /Address/Edit/5

   42         <AcceptVerbs(HttpVerbs.Post)> _

   43         Public Function Edit(ByVal addressId As Integer, ByVal customerId As Integer, ByVal collection As FormCollection) As ActionResult

   44             Try

   45                 Dim addressViewData As New AddressViewData()

   46                 addressViewData.Address = Me.repository.GetAddressById(addressId)

   47                 UpdateModel(addressViewData)

   48                 Me.repository.UpdateAddress()

   49                 Return RedirectToAction("Info", "Customer", New With {.id = customerId})

   50             Catch

   51                 Return View()

   52             End Try

   53         End Function

   54 

   55         '

   56         ' POST: /Address/Edit/5

   57         <HttpPost()> _

   58         Function Edit(ByVal id As Integer, ByVal collection As FormCollection) As ActionResult

   59             Try

   60                 ' TODO: Add update logic here

   61 

   62                 Return RedirectToAction("Index")

   63             Catch

   64                 Return View()

   65             End Try

   66         End Function

   67 

   68         '

   69         ' GET: /Address/Delete/5

   70         Public Function Delete(ByVal addressId As Integer, ByVal customerId As Integer) As ActionResult

   71             Dim address = Me.repository.GetAddressById(addressId)

   72             Me.repository.DeleteAddress(address, customerId)

   73             Return RedirectToAction("Info", "Customer", New With {.id = customerId})

   74         End Function

   75 

   76         '

   77         ' POST: /Address/Delete/5

   78         <HttpPost()> _

   79         Function Delete(ByVal id As Integer, ByVal collection As FormCollection) As ActionResult

   80             Try

   81                 ' TODO: Add delete logic here

   82 

   83                 Return RedirectToAction("Index")

   84             Catch

   85                 Return View()

   86             End Try

   87         End Function

   88     End Class

   89 

   90 End Namespace


이젠 방금전에 만든 CustomerController 의 info 함수에 연결할 View 를 생성할겁니다.
함수상에서 오른쪽 클릭을 하면 나타나는 Context Menu 에서 "Add View..." 를 클릭합니다.
참 쉽죠~~잉? ^^


ComboBox 에서 DataClass를 입력합니다.



만들어진 View 를 아래와 같이 수정합니다.

   40 <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

   41     <h2>

   42         Customer Information</h2>

   43     <fieldset>

   44         <p>

   45             CompanyName:

   46             <%= Html.Encode(Model.CompanyName) %>

   47         </p>

   48         <p>

   49             EmailAddress:

   50             <%= Html.Encode(Model.EmailAddress) %>

   51         </p>

   52         <p>

   53             Name:

   54             <%= Html.Encode(Model.Title + " " + Model.FirstName + " " + Model.MiddleName + Model.LastName) %>

   55         </p>

   56         <p>

   57             Phone:

   58             <%= Html.Encode(Model.Phone) %>

   59         </p>

   60     </fieldset>

   61         <h3>

   62         Addresses</h3>

   63     <ul>

   64         <% For Each address In Model.CustomerAddress%>

   65         <li>

   66             <%= address.Address.AddressLine1 + " " + address.Address.AddressLine2 + " - " + address.Address.City %>

   67             <%= Html.ActionLink("(Edit)", "Edit", "Address", New With {address.AddressID, Model.CustomerID}, Nothing)%>

   68             <%= Html.ActionLink("(Delete)", "Delete", "Address", New With {address.AddressID, Model.CustomerID}, Nothing)%>

   69         </li>

   70         <% Next%>

   71     </ul>

   72     <%= Html.ActionLink("Add New Address", "Create", "Address", New With {Model.CustomerID}, Nothing)%>

   73 

   74 </asp:Content>

   75 


CustomerController 와 AddressController 에 포함되어있는 ActionMethod 에 대한 View 를 같은 방식으로 만듭니다.
그 내용은 유사하므로 이곳에 설명하진 않습니다.


F5 를 클릭해서 Web Application을 실행해 봅니다.

Customer 링크를 클릭해서 리스트를 확인하고...
Page 를 이동해 봅니다.
Address 추가도 해보고...

수정/삭제도 해봅니다.


너무 쉬운내용을 어렵게(설명한답시고 캡쳐하고, 주저리주저리) 풀어 놓은것 아닌가 합니다.
어느정도 MVC2 Application 을 작성하는 방식에 대해서 살펴보았습니다.

기본적으로 구성하는 것. 한마디로 참 쉽죠~~잉... ^^'
허지만 세상일이란 기본적인것만 있지 않죠... ㅋㅋ

여튼 이후에는 이 이외에 Unit Test 를 설정하고, Area 를 구성하는 것도 살펴보려합니다.
자세한 사항에 대해서 살펴보기전까지는 기본 구성에 대해서만 진행할려구요...

 
행복한 고수되십시요.


woojja ))*
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
반응형

+ Recent posts