@ Html.validate how does it work?

I wrote this code for form validation and it works fine

Data holder

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace workflow.DataHolders
{
    public class NewCompany
    {
        [Required]
        [StringLength(200, MinimumLength = 3, ErrorMessage="Length Of The Company Name Should  Be More Than Three Letters")]
        public string CompanyName { get; set; }

        [Required]
        [EmailAddress(ErrorMessage="Invalid Email Address")]
        public string Email { get; set; }

        [Required]
        [StringLength(200, MinimumLength = 2, ErrorMessage = "Length Of The Country Name Should  Be More Than Two Letters")]
        public string Country { get; set; }

        public string Description { get; set; }
    }
}

      

View side

<link href="@Url.Content("~/sharedfiles/css/forms/addnew.css")" rel="stylesheet" type="text/css" />
<div id="Add_container">

   @if (!ViewData.ModelState.IsValid)
   {
       <div id="validationMessage">Please Correct The Errors Below</div> 
   }

    @using (Html.BeginForm("ValidateAndSignUp", "Accounts", FormMethod.Post))
    {
        @Html.AntiForgeryToken()

        @Html.ValidationMessage("CompanyName");
        <span class="field_title">Company Name: </span>
        @Html.TextBox("CompanyName")

        @Html.ValidationMessage("Email");
        <span class="field_title">Email: </span>
        @Html.TextBox("Email")



        @Html.ValidationMessage("Country");
        <span class="field_title">Country Name: </span>
        @Html.TextBox("Country")

        <span class="field_title">About The Company: </span>
        @Html.TextArea("Description")



        <input type="submit" value="Create New Account">


    }
</div>
<div class="get_connected_message">
    <h1>Get Connected with your Customers</h1>
</div>

<div class="get_connected_message">
    <h1>Build your profissional buisness world</h1>
</div>

<div class="clear"></div>

      

ValidateAndSignUp Action

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ValidateAndSignUp(NewCompany newCompany)
{
    if (ModelState.IsValid)
    {

        return RedirectToAction("index", "Home");
    }
    else
    {
        return View("signUp", newCompany);
    }
}

      

Everything works fine and it validates correctly, but I tried to use the @ Html.validate method because I read that it doesn't register anything in this field for validation, but what does that mean? does this mean that it is registering it for client side validation, and if so how can this be done?

+3


source to share


1 answer


@ Html.Validate registers a property for validation without having to display a message if validation fails. This HTML helper is only used when you want to initiate validation, but don't want to show a specific error for this control. In your case, since you are already using ValidationMessage, there is no need to use Validate; because the property has already been registered for validation in the ValidationMessage helper.



Also, if you have unobtrusive JavaScript enabled, @ Html.Validate will just return without actually doing anything. Because if you have unobtrusive Javascript, then other mechanisms will take care of registering the property for client and server validation.

0


source







All Articles