Click here to Skip to main content
15,921,226 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hiiiiiii
i am new to mvc i tried datavalidations by taking model class1.
C#
public class Class1
   {
       [Required(ErrorMessage = "name is required")]
       public string name { set; get; }
       [Required(ErrorMessage = "id is required")]
       public string id{set;get;}
       [Required(ErrorMessage = "gender is required")]
       public string gender{set;get;}
       [Required(ErrorMessage = "hobbies is required")]
       public string hobbies { set; get; }

   }


i taken controller and added a view with create scafflonding.i added scripts in view.
XML
<head runat="server">
 <script src="../../Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script src="../../Scripts/jquery.validate.js" type="text/javascript"></script>
    <script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
    <script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
    <script src="../../Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>
  <% Html.EnableClientValidation();%>
    <title>Index</title>
</head


i am not getting any validation error.can any one help me please.
the view is default generated.
XML
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MvcApplication1.Models.Class1>" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
 <script src="../../Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script src="../../Scripts/jquery.validate.js" type="text/javascript"></script>
    <script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
    <script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
    <script src="../../Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>
  <% Html.EnableClientValidation();%>
    <title>Index</title>
</head>
<body>
     <% using (Html.BeginForm()) {%>
       <%-- <%: Html.ValidationSummary(true," unsuccessfull pleses fill correctly") %>--%>

        <fieldset>
            <legend>Fields</legend>

            <div class="editor-label">
                <%: Html.LabelFor(model => model.name) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.name) %>
                <%: Html.ValidationMessageFor(model => model.name) %>
            </div>

            <div class="editor-label">
                <%: Html.LabelFor(model => model.id) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.id) %>
                <%: Html.ValidationMessageFor(model => model.id) %>
            </div>

            <div class="editor-label">
                <%: Html.LabelFor(model => model.gender) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.gender) %>
                <%: Html.ValidationMessageFor(model => model.gender)%>
            </div>

            <div class="editor-label">
                <%: Html.LabelFor(model => model.hobbies) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.hobbies) %>
                <%: Html.ValidationMessageFor(model => model.hobbies)%>
            </div>

            <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>

    <% } %>

    <div>
        <%: Html.ActionLink("Back to List", "Index") %>
    </div>

</body>
</html>
Posted
Updated 18-Nov-13 22:56pm
v2
Comments
♥…ЯҠ…♥ 19-Nov-13 4:36am    
Can you post html controls also?
laxmi smiley 19-Nov-13 5:21am    
i want client side validation that is required field validation defined in model before saving to database.

Hi Lakshmi,

After seeing your code everything seems to be perfect, except controller.Since you missed to post it here.

In your action do you have modelstate.isValid like this
SQL
public ActionResult actionName(class1 clsObj)
{
  if(ModelState.IsValid)
   {
   // TODO: Add your own logic here
   }
  else
   {
    return View(clsObj);
   }
}

Updated solution after your comments,
XML
Hey, in ASP.NET MVC3, there's no need to add Html.EnableClientValidation() in view page, instead, just enable the clientValidation in the webconfig file as below:
<appSettings>
  <add key="ClientValidationEnabled" value="true" />
  <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>

Hope this helps you a bit.

Regards,
RK
 
Share this answer
 
v2
Hi

add class as
@class = "required"

Ex:- <%= Html.TextBoxFor(model => model.LastName, new { @class = "required" })%>

Try this hope this will help
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900