ASP.NET MVC 4 View ( Razor ) + EditorTemplates + Model Binding + Form Validation 方法

 
ASP.NET MVC 4 View ( Razor ) + EditorTemplates + Model Binding + Form Validation 方法

   /Models/News.cs
 
   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Web;
   using System.Data.Entity;
   using System.ComponentModel.DataAnnotations;
   using System.ComponentModel.DataAnnotations.Schema;
   using System.Globalization;
   using System.Web.Security;
 
   namespace MvcApplication.Models
   {
      public class News
      {
         [Key]
         [Required]
         public int News_ID { get; set; }
         [Display(Name="News Header")]
         [Required(ErrorMessage = "News Header Required")]
         public string News_Name { get; set; }
         [Display(Name = "News Content")]
         [Required(ErrorMessage = "News Content Required")]
         public string News_Content { get; set; }
         [Required]
         public System.DateTime News_Date { get; set; }
      }
   }
 
   /Controllers/NewsController.cs
 
   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Web;
   using System.Web.Mvc;
   using ProductServiceLibrary;
   using AutoMapper;
   using MvcApplication.Models;
 
   namespace MvcApplication.Controllers
   {
      public class NewsController : Controller
      {
 
         readonly INewsRepository repository;
 
         public NewsController(INewsRepository repository)
         {
            this.repository = repository;
         }
 
         … …
 
         public ActionResult Add()
         {
            return View("Edit");
         }
 
         [HttpPost]
         public ActionResult Add(Models.News News)
         {
            if (ModelState.IsValid)
            {
               repository.Add(Mapper.Map(News, new ProductServiceLibrary.News()));
            }
            return RedirectToAction("Index");
         }
 
         public ActionResult Edit(int? id)
         {
            if(id.HasValue){
               return View(Mapper.Map(repository.Get(id.Value), new Models.News()));
            }else{
               return RedirectToAction("Index");
            }
         }
 
         [HttpPost]
         public ActionResult Edit(Models.News News)
         {
            if (ModelState.IsValid)
            {
               repository.Edit(Mapper.Map(News, new ProductServiceLibrary.News()));
            }
            return RedirectToAction("Index");
         }
 
         … …
 
      }
   }
 
   /Views/Edit.cshtml
 
   @model MvcApplication.Models.News
 
   <style>
 
      input.input-validation-error, textarea.input-validation-error {
         border: 1px solid #e80c4d;
      }
 
   </style>
 
   @if(Model != null){
      using (Html.BeginForm("Edit", "News")) {
         @Html.AntiForgeryToken()
         @Html.ValidationSummary(true)
         @Html.EditorForModel("EditNews")
      }
   }
   else
   {
      using (Html.BeginForm("Add", "News")) {
         @Html.AntiForgeryToken()
         @Html.ValidationSummary(true)
         @Html.EditorForModel("AddNews")
      }
   }
 
   <div>
      @Html.ActionLink("Back to List", "Index")
   </div>
 
   @section Scripts {
      @Scripts.Render("~/bundles/jqueryval")
   }
 
   /Views/Shared/EditorTemplates/AddNews.cshtml
 
   @model MvcApplication.Models.News
 
   <fieldset>
 
      <legend>News</legend>
 
      <div class="editor-label">
         @Html.LabelFor(model => model.News_Name)
      </div>
      <div class="editor-field">
         @Html.TextBoxFor(model => model.News_Name, null, new { style = "width:100%;" })
         @Html.ValidationMessageFor(model => model.News_Name)
      </div>
 
      <div class="editor-label">
         @Html.LabelFor(model => model.News_Content)
      </div>
      <div class="editor-field">
         @Html.TextAreaFor(model => model.News_Content, new { style = "width:100%;", rows = "10" })
         @Html.ValidationMessageFor(model => model.News_Content)
      </div>
 
      <p>
         <input type="submit" value="Create" />
      </p>
 
   </fieldset>
 
   /Views/Shared/EditorTemplates/EditNews.cshtml
 
   @model MvcApplication.Models.News
 
   <fieldset>
 
      <legend>News</legend>
 
      @Html.HiddenFor(model => model.News_ID)
 
      <div class="editor-label">
         @Html.LabelFor(model => model.News_Name)
      </div>
      <div class="editor-field">
         @Html.TextBoxFor(model => model.News_Name, null, new { style = "width:100%;" })
         @Html.ValidationMessageFor(model => model.News_Name)
      </div>
 
      <div class="editor-label">
         @Html.LabelFor(model => model.News_Content)
      </div>
      <div class="editor-field">
         @Html.TextAreaFor(model => model.News_Content, new { style = "width:100%;", rows = "10" })
         @Html.ValidationMessageFor(model => model.News_Content)
      </div>
 
      <p>
         <input type="submit" value="Save" />
      </p>
 
   </fieldset>