Reputation: 33
Please tell me why ComponentModel.DataAnnotations is not working here i have done every thing but in vain so tell me where i have mistakes in this code. here i am posting my view along with model.
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<form>
<div id="par" class="form-horizontal">
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.BrandCode, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.BrandCode, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.BrandCode, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ProductSubGroupCode, new { @class = "control-label col-md-2 col-ld-2 col-sd-2" })
<div class="col-md-10">
@(Html.Kendo().ComboBoxFor(model => model.ProductSubGroupCode)
.DataTextField("ProductSubGroupName")
.DataValueField("ProductSubGroupCode")
.DataSource(d => d.Read(r => r.Action("GetProductSubGroup", "Product")))
.Placeholder("Select Product Sub Group...")
.Suggest(true)
.HighlightFirst(true)
)
@Html.ValidationMessageFor(model => model.ProductSubGroupCode, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ProductGroupCode, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@(Html.Kendo().ComboBoxFor(model => model.ProductGroupCode)
.MinLength(100)
.DataTextField("ProductGroupName")
.DataValueField("ProductGroupCode")
.DataSource(d => d.Read(r => r.Action("GetProductGroup", "Product")))
.Placeholder("Select Product Group...")
.Suggest(true)
.HighlightFirst(true)
)
@Html.ValidationMessageFor(model => model.ProductGroupCode, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.BrandName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.BrandName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.BrandName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.BrandDescription, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.BrandDescription, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.BrandDescription, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Active, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.CheckBoxFor(model => model.Active)
@Html.ValidationMessageFor(model => model.Active, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.SortOrder, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.SortOrder, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.SortOrder, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" id="Save" class="btn btn-info" />
<p id="content"></p>
</div>
</div>
</form>
This is the model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
public class BrandViewModels
{
[Display(Name = "Brand Code")]
[Required(ErrorMessage = "Brand Code is required")]
[RegularExpression(@"(\S)+", ErrorMessage = "White space is not allowed.")]
[Remote("IsUniqueBrandCode", "Product", AdditionalFields = "BrandCode", HttpMethod = "POST", ErrorMessage = "Brand Code already exists.")]
public string BrandCode { get; set; }
public int CompanyId { get; set; }
public List<ProductSubGroupList> ProductSubGroupList { get; set; }
public List<ProductGroupList> ProductGroupList { get; set; }
[Required(ErrorMessage = "Please select product group")]
[Display(Name = "Product Group")]
public string ProductGroupCode { get; set; }
[Required(ErrorMessage = "Please select product sub group")]
[Display(Name = "Product Sub Group")]
public string ProductSubGroupCode { get; set; }
[Required(ErrorMessage = "Brand name is required")]
[Display(Name = "Brand Name")]
public string BrandName { get; set; }
[Required(ErrorMessage = "Description is required")]
[Display(Name = "Description")]
public string BrandDescription { get; set; }
[Display(Name = "Active")]
public bool Active { get; set; }
[Display(Name = "Sort Order")]
[Required(ErrorMessage = "Sorting order is required")]
public int? SortOrder { get; set; }
}
Upvotes: 1
Views: 2611
Reputation: 1
Here is my code Working perfect i seen your Code you may have these Error,
@using (Html.BeginForm())
instead of <form>
@Html.ValidationSummary(false)
in place of @Html.ValidationSummary(true)
please see my Code.
<div class="container">
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>mymodel</h4>
<hr />
@Html.ValidationSummary(false)
<div class="form-group">
@Html.LabelFor(model => model.id, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.id)
@Html.ValidationMessageFor(model => model.id)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.name, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.name)
@Html.ValidationMessageFor(model => model.name)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
</div>
<div>
@Html.ActionLink("Back to List", "Index")
</div>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
Upvotes: 0
Reputation: 541
Please check if you have following Appsettings in Web Config
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
Please refer Data Anotation validation
Upvotes: 1