2014-02-27 23 views
2

我在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") 
}*@ 

,,沒有驗證消息....幫助是明顯的

+0

你說出來的範圍 –

回答

2

@{ Html.EnableClientValidation(); }在您的視圖。如果這樣,那麼意味着在web.config文件中關閉了驗證。

另外,請確認您的腳本包含在接下來的順序:

  1. jQuery的
  2. jquery.validate.js
  3. jquery.validate.unobtrusive.js
+0

(jquery.validate.js和jquery.validate.unobtrusive.js)凡添加有鑑於此 –

+0

@Jatt .net之前@using(Html.BeginForm())' –

+0

對不起同樣的問題 –

1

Web.config文件

<configuration> 
    <appSettings> 
     <add key="ClientValidationEnabled" value="true"/> 
     <add key="UnobtrusiveJavaScriptEnabled" value="true"/> 
    </appSettings> 

無論是在Globa l.asax文件

HtmlHelper.ClientValidationEnabled = true; 
HtmlHelper.UnobtrusiveJavaScriptEnabled = true; 

確保JS文件

+0

完成這些太..但不解決問題尚未:( –

+0

通過添加jquery refrence解決..謝謝alejsej .. –