1. 程式人生 > >MVC模型繫結

MVC模型繫結

(一)理解模型繫結

模型繫結是HTTP請求與C#方法之間的一個橋樑,它根據 Action 方法中的 Model 型別建立 .NET 物件,並將 HTTP 請求資料經過轉換賦給該物件。

(二)預設的模型繫結器

應用程式有多個繫結器,大多數的都是依賴與內建繫結器類---DefaultModelBinder,一般情況下,模型繫結器搜尋4個路徑,如下所示。

描述
Request.Form HTML表單提供的值
RouteDate.Values 使用應用程式路由獲取的值
Request.QueryString 包含在URL的請求字串裡面的資料
Request.Files 作為請求部分被上傳的檔案

 

(三)程式碼示範

1.首先在Models下面建立Person類

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVCModels.Models
{
    public class Person
    {
        public int PersonId { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }

        public DateTime BirthDate { get; set; }

        public Address HomeAddress { get; set; }

        public Role Role { get; set; }
    }

    public enum Role
    {
        Admin,
        User,
        Guest,
    }

    public class Address
    {
        public string Line { get; set; }

        public string City { get; set; }

        public string PostalCode { get; set; }

        public string Country { get; set; }
    }
}

2.在Controllers中的HomeController中編寫如下程式碼

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCModels.Models;


namespace MVCModels.Controllers
{
    public class HomeController : Controller
    {
        private Person[] personDate = {
            new Person { PersonId = 1, FirstName = "Adam", LastName = "Freeeman", Role = Role.Admin},

             new Person { PersonId = 2, FirstName = "Jacqui", LastName = "Griffyth", Role = Role.User},

              new Person { PersonId = 1, FirstName = "John", LastName = "Smith", Role = Role.Guest},
        };

        public ActionResult Index( int id = 1 )
        {
            Person dataItem = personDate.Where(p => p.PersonId == id).First();

            return View(dataItem);
        }

        public ActionResult CreatePerson()
        {
            return View(new Person());
        }

        [HttpPost]
        public ActionResult CreatePerson(Person model)
        {
            return View("Index",model);
        }
    }
}

3.在檢視(Views)中新增createPerson檢視,程式碼示例如下

@model MVCModels.Models.Person
@{
    ViewBag.Title = "CreatePerson";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>CreatePerson</h2>

@using (Html.BeginForm())
{
    <div>
        <label>
            @Html.LabelFor(m => m.PersonId);
            @Html.LabelFor(m => m.PersonId);
        </label>
    </div>

    <div>
        <label>
            @Html.LabelFor(m => m.FirstName);
            @Html.EditorFor(m => m.FirstName);
        </label>
    </div>

    <div>
        <label>
            @Html.LabelFor(m => m.LastName);
            @Html.EditorFor(m => m.LastName);
        </label>
    </div>

    <div>
        <label>
            @Html.LabelFor(m => m.Role);
            @Html.EditorFor(m => m.Role);
        </label>
    </div>

    <button type="submit">Submit</button>

}

4.最後在index檢視中再進行程式碼編寫,程式碼示例如下

@model MVCModels.Models.Person
@{ 
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Person</h2>
<div>
    <label>ID:</label>
    @Html.DisplayFor(m => m.PersonId)
</div>

<div>
    <label>First Name:</label>
    @Html.DisplayFor(m => m.FirstName)

</div>

<div>
    <label>Role</label>
    @Html.DisplayFor(m => m.Role)
</div>

以上的程式碼完全編寫完了之後,就可以進行運行了