0

正如标题所说,我的必填字段不会触发。

这是我的 ViewModel 类,它的名字是SignUpForm.cs

public class SignUpForm
{
    [Required]
    [StringLength(100,ErrorMessage="You must enter a your firstname!", 
    MinimumLength = 1)]
    public string FirstName { get; set; }
    [Required]
    [StringLength(100, ErrorMessage = "You must enter a your surname!", 
    MinimumLength = 1)]
    public string SurName { get; set; }      
}

这是我的剃刀观点!视图名称为Step2.cshtml

@model MvcApplication2.Models.ViewModel.SignUpForm
@{
ViewBag.Title = "Step2";
 }

<div id="content-header">
<h1 class="title">Complete your registration</h1>   
</div>  
<div id="content-area">
    <h4>Fill in the form below to complete your registration
    </h4>
@using(Html.BeginForm("testar","Interested", FormMethod.Post))
{


<h3>Personal information</h3>
<p class="app-label">Surname<span class="requiredField">*</span></p>
<div class="editor-label">
    @Html.EditorFor(model => model.FirstName)
    @Html.ValidationMessageFor(m => m.FirstName)
</div>
<p class="app-label">Surname<span class="requiredField">*</span></p>
<div class="editor-label">
    @Html.EditorFor(model => model.SurName)
    @Html.ValidationMessageFor(m => m.SurName)
</div>
}

按要求提供来源

<script src="/Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
<script src="/Scripts/modernizr-2.5.3.js" type="text/javascript"></script>
<link href="/Content/remigium.css" rel="stylesheet" />


 <h3>Personal information</h3>
 First name<span class="requiredField">*</span>
<div class="editor-label">
<input class="text-box single-line" data-val="true" data-val-length="You must enter a your firstname!" data-val-length-max="100" data-val-length-min="1" data-val-required="The FirstName field is required." id="FirstName" name="FirstName" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="FirstName" data-valmsg-replace="true"></span>
</div>
<p class="app-label">Surname<span class="requiredField">*</span></p>
<div class="editor-label">
<input class="text-box single-line" data-val="true" data-val-length="You must enter a your surname!" data-val-length-max="100" data-val-length-min="1" data-val-required="The SurName field is required." id="SurName" name="SurName" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="SurName" data-valmsg-replace="true"></span>
</div>
4

1 回答 1

2

我错过了这些脚本参考,我添加了它们,一切都很酷!

<script src="/Scripts/jquery.validate.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
于 2013-02-14T23:12:14.360 回答