MVC2中自訂校正

來源:互聯網
上載者:User

建立一個Mvc2的應用程式;

在Models 檔案夾下建立一個類EmailAttribute

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.ComponentModel.DataAnnotations;
6
7 namespace MvcTemp.Models
8 {
9 public class EmailAttribute: RegularExpressionAttribute
10 {
11 public EmailAttribute()
12 : base(@"^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$")
13 {
14 }
15 }
16 }

重新編譯下項目,下面示範如何自訂使用這個標記

在Model檔案夾下建立一個類Student

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using MvcTemp.Models;
namespace MvcTemp.Model
{
public class Student
{
private int _id;
[Required(ErrorMessage="必填")]
public int Id
{
get { return _id; }
set { _id = value; }
}
private string _name;
[Required(ErrorMessage="必填")]
public string Name
{
get { return _name; }
set { _name = value; }
}
private string _emailAddress;
[Email(ErrorMessage="錯誤的郵件地址")]
public string EmailAddress
{
get { return _emailAddress; }
set { _emailAddress = value; }
}
}
}

重新編譯下項目,添加Controllers,

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;namespace MvcTemp.Controllers{    public class StudentController : Controller    {        //        // GET: /Student/        public ActionResult Index()        {            return View();        }        //        // GET: /Student/Details/5        public ActionResult Details(int id)        {            return View();        }        //        // GET: /Student/Create        public ActionResult Create()        {            return View();        }         //        // POST: /Student/Create        [HttpPost]        public ActionResult Create(FormCollection collection)        {            try            {                // TODO: Add insert logic here                return RedirectToAction("Index");            }            catch            {                return View();            }        }                //        // GET: /Student/Edit/5         public ActionResult Edit(int id)        {            return View();        }        //        // POST: /Student/Edit/5        [HttpPost]        public ActionResult Edit(int id, FormCollection collection)        {            try            {                // TODO: Add update logic here                 return RedirectToAction("Index");            }            catch            {                return View();            }        }        //        // GET: /Student/Delete/5         public ActionResult Delete(int id)        {            return View();        }        //        // POST: /Student/Delete/5        [HttpPost]        public ActionResult Delete(int id, FormCollection collection)        {            try            {                // TODO: Add delete logic here                 return RedirectToAction("Index");            }            catch            {                return View();            }        }    }}

  在Global.asax頁中註冊自己定義的標記

View Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using MvcTemp.Models;
using System.ComponentModel.DataAnnotations;

namespace MvcTemp
{
// 注意: 有關啟用 IIS6 或 IIS7 傳統模式的說明,
// 請訪問 http://go.microsoft.com/?LinkId=9394801

public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
"Default", // 路由名稱
"{controller}/{action}/{id}", // 帶有參數的 URL
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 參數預設值
);

}

protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(EmailAttribute), typeof(RegularExpressionAttributeAdapter));
RegisterRoutes(RouteTable.Routes);
}
}
}

   最後建立Student的Action視圖即可

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.