首先我們為一個 form 的頁面 (一個 view) 定義以下 ViewModel:
1 2 3 4 5 6 7 8 9 10 11 | using ReallyTrue.Models; using System.Collections.Generic; namespace ReallyTrue.ViewModel { public class NewCustomerViewModel { public Customer Customer { get ; set ; } public IEnumerable<Membershiptype> MembershipTypes { get ; set ; } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | namespace ReallyTrue.Models { public class Customer { public int Id { get ; set ; } [Required] [StringLength(255)] public string Name { get ; set ; } public bool IsSubscribedToNewsletter { get ; set ; } public DateTime? Birthday { get ; set ; } public MembershipType MembershipType { get ; set ; } public int ? MembershipTypeId { get ; set ; } } } |
1 2 3 4 5 6 7 8 9 10 11 | namespace ReallyTrue.Models { public class MembershipType { public int Id { get ; set ; } public short SignUpFee { get ; set ; } public string Name { get ; set ; } public byte DurationInMonths { get ; set ; } public byte DiscountRate { get ; set ; } } } |
1 2 3 4 5 6 7 8 9 | public ActionResult New() { var membershipTypes = _context.MembershipTypes.ToList(); var viewModel = new NewCustomerViewModel() { MembershipTypes = membershipTypes }; return View(viewModel); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | @model ReallyTrue.ViewModel.NewCustomerViewModel @{ ViewBag.Title = "New Customer" ; Layout = "~/Views/Shared/_Layout.cshtml" ; } <h2>New Customer</h2> @ using (Html.BeginForm( "Create" , "Home" )) { <div class = "form-group" > @Html.LabelFor(m => m.Customer.Name) @Html.TextBoxFor(m => m.Customer.Name, new { @ class = "form-control" }) </div> <div class = "form-group" > <label>Date of Birth</label> @Html.TextBoxFor(m => m.Customer.Birthday, new { @ class = "form-control" }) </div> <div class = "checkbox" > <label> @Html.CheckBoxFor(m => m.Customer.IsSubscribedToNewsletter) Subscribed to Newsletter? </label> </div> <div class = "form-group" > <label>Membership Type</label> @Html.DropDownListFor(m => m.Customer.MembershipTypeId, new SelectList(Model.MembershipTypes, "Id" , "Name" ), "Select" , new { @ class = "form-control" }) </div> <button type= "submit" class = "btn btn-primary" >Save</button> } |
其中 save 按下時,它會把 form data 以 post method 方式傳送到 /home/create 這個 route。因此,我們定義 home controller 中的 create method 為 (attribute 為 HttpPost)
1 2 3 4 5 | [HttpPost] public ActionResult Create(Customer customer) { return new EmptyResult(); } |
還有一些 form 的 key concept 要學,因為暫時沒有機會應用到所以也不詳細做紀錄了。
- Client side: @Html.ValidationMessageFor(m => m,Customer.Name),及 render jquery 的 validation bundle。
Server side 的簡易 validation:
在 Create method 中寫上
12345678if
(!ModelState.IsValid)
{
//do something, say return View(New ViewModel
//{ Customer = customer,
// MembershipTypes = _context.MembershipTypes.ToList()
//}
//)
}
- Data Annotation: [Required], [StringLength(int)], [Range(1,2019)], [Phone], [EmailAddress], [Url], [RegularExpression("...")], ...
- Anti-forgery Token 用來防止 malicious attack。
No comments:
Post a Comment