8bitcat
8bitcat

Reputation: 2234

ASP.NET MVC [Required] does not trigger

As the title says my Required field does not trigger.

This is my ViewModel class its name is 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; }      
}

This is my Razor view! Name of view is 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>
}

Source as Requested

<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>

Upvotes: 0

Views: 473

Answers (1)

8bitcat
8bitcat

Reputation: 2234

I was missing these script references, I added them and all was cool!

<script src="/Scripts/jquery.validate.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>

Upvotes: 2

Related Questions