【发布时间】:2014-02-27 14:37:49
【问题描述】:
我正在 VS2012 中制作一个示例 MVC4 项目.. 我想在文本框字段上进行一些验证..但不幸的是它从来没有工作过,我正在发布我的文件,
我的朋友控制器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using validators.Models;
namespace validators.Controllers
{
public class FriendController : Controller
{
//
// GET: /Friend/
public ActionResult Create()
{
return View();
}
}
}
我的模型类 person.cs
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace validators.Models
{
public class person
{
[Required(ErrorMessage="must")]
[StringLength(10, ErrorMessage="blah")]
public string Firstname { get; set; }
[Required]
public string Lastname { get; set; }
}
}
我正在添加强类型视图 .... 生成以下 create.cshtml
@model validators.Models.person
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>person</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Firstname)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Firstname)
@Html.ValidationMessageFor(model => model.Firstname)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Lastname)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Lastname)
@Html.ValidationMessageFor(model => model.Lastname)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@*@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}*@
当我运行这个项目并在文本框中输入没有任何值的提交按钮时,没有验证消息....帮助很明显
【问题讨论】:
-
你说的超出范围
标签: c# asp.net-mvc asp.net-mvc-4 validation data-annotations