我在VS2012拍樣片MVC4項目..我想要在文本框中領域的一些驗證..但不幸的是它從不工作,,我張貼我的文件,驗證不列入在MVC4項目工作
我FriendController
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")
}*@
,,沒有驗證消息....幫助是明顯的
你說出來的範圍 –