Mvc3提交表格验证

Model层:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
namespace MvcApplication1.Models
{
    public class User
    {
        public int ID { get; set; }


        [DisplayName("姓名")]
        [Required(ErrorMessage = "姓名不能为空")]
        public string Name { get; set; }

        [DisplayName("密码")]
        [Required(ErrorMessage = "密码不及为空")]
        [RegularExpression(@"^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$", ErrorMessage = "请输入正确的Email格局\n示例:abc@123.com")]      
        //[StringLength(10, MinimumLength = 6, ErrorMessage = "密码6-10位")]
        public string Password { get; set; }
    }
}

Control层:
using System.Web.Mvc;
using MvcApplication1.Models;

namespace MvcApplication1.Controllers
{
    public class UserController : Controller
    {

        public ActionResult Create()
        {

            return View();

        }
        [HttpPost]
        public ActionResult Create(User model)
        {
            if (ModelState.IsValid)
            {
                var a = "";
            }
            return View(model);

        }
    }
}

UI层:
@model MvcApplication1.Models.User

@{
    ViewBag.Title = "Create";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
    <div>
        <fieldset>
            <legend>Account Information</legend>

            <div class="editor-label">
                @Html.LabelFor(m => m.Name)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(m => m.Name)
                @Html.ValidationMessageFor(m => m.Name)
            </div>

            <div class="editor-label">
                @Html.LabelFor(m => m.Password)
            </div>
            <div class="editor-field">
             @Html.TextBoxFor(m => m.Password)
                @*@Html.PasswordFor(m => m.Password)*@
                @Html.ValidationMessageFor(m => m.Password)
            </div>

            <p>
                <input type="submit" value="Log Out" />
            </p>
        </fieldset>
    </div>
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值