ASP.NET/MVC 3

ASP.NET MVC 3 Model 추가하기

littlemk 2018. 8. 23. 15:14

ASP.NET MVC 3 Model 추가하기

Visual Studio 2010 express

  1. Models 폴더 -> 우클릭 -> Add -> Class 클릭
  • 모델 파일명 :: Test0821_Model.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Test0821.Models
{
    public class Test0821_Model
    {
        public string ID { get; set; }
        public string NAME { get; set; }
        public int AGE { get; set; }
        public string SEX { get; set; }
    }
}


  1. Index.cshtml 코드 변경
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
       <title></title>
</head>
<body>
       <div>
              안?E녕?c하I세???요?a! Index.cshtml 뷰?a 페?a이I지o입O니?I다?U.
       <br>
       @ViewBag.Hello
       <br>
       @* ("보???여???질u 이I름???", "실?C행a될?E 액???션?C")*@
       @Html.ActionLink("NewPage", "NewForm0821");
       </div>
</body>
</html>


  1. Test0821Controller.cs 코드 변경
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Test0821.Controllers
{
    public class Test0821Controller : Controller
    {
        //
        // GET: /Home/
       // public ActionResult Index()
        //{
            //return View();
        //}
        // 홈??페?a이I지o상?o에???서??? 보???여???질u 문???구???
        //public string Index()
        //{
            //return "안?E녕?c하I세???요?a~ HomeController 실?C행a했??습?A니?I다?U.";
        //}
        // View 추??가???하I기?a
        public ViewResult Index()
        {
            string str_Message = "동???적u 출a력?A하I는?A 부?I분???입O니?I다?U.";
            ViewBag.Hello = str_Message;
            return View();
        }
        [HttpGet]
        public ViewResult NewForm0821()
        {
            return View();
        }
    }
}



  1. ViewResult NewForm0821() 메서드에서 Add to View 클릭


  1. NewForm0821.cshtml 코드 변경
@model Test0821.Models.Test0821_Model
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <title>NewForm0821</title>
</head>
<body>
    <div>
              @using (Html.BeginForm())
        {
            <p>ID : @Html.TextBoxFor(a => a.ID )</p>
            <p>이I름??? : @Html.TextBoxFor(a => a.NAME)</p>
            <p>나???이I : @Html.TextBoxFor(a => a.AGE)</p>
            <p>
                성???별??? @Html.DropDownListFor(a => a.SEX, new[]{
                                            new SelectListItem() {Text = "남???자U", Value="M", Selected = true },
                                            new SelectListItem() {Text = "여???자U", Value="W"}
                                        })
            </p>
            <input type="submit" value="확??인I" />
        }
    </div>
</body>
</html>


  1. 디버깅 결과



  1. Test0821Controller 코드 수정
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Test0821.Models;
namespace Test0821.Controllers
{
    public class Test0821Controller : Controller
    {
        //
        // GET: /Home/
       // public ActionResult Index()
        //{
            //return View();
        //}
        // 홈??페?a이I지o상?o에???서??? 보???여???질u 문???구???
        //public string Index()
        //{
            //return "안?E녕?c하I세???요?a~ HomeController 실?C행a했??습?A니?I다?U.";
        //}
        // View 추??가???하I기?a
        public ViewResult Index()
        {
            string str_Message = "동???적u 출a력?A하I는?A 부?I분???입O니?I다?U.";
            ViewBag.Hello = str_Message;
            return View();
        }
        [HttpGet]
        public ViewResult NewForm0821()
        {
            return View();
        }
        [HttpPost]
        public ViewResult NewForm0821(Test0821_Model dataModel)
        {
            return View("dataViewPage", dataModel);
        }
    }
}


  1. dataViewPage 생성 후 코드 변경
@model Test0821.Models.Test0821_Model
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <title>dataViewPage</title>
</head>
<body>
    <div>
        <p>@Model.ID</p>
              <p>@Model.NAME</p>
              <p>@Model.AGE</p>
              <p>@Model.SEX</p>
    </div>
</body>
</html>


  1. 디버깅 결과