Dear All,
I have just downloaded the asp.net MVC product.
I started with the validation using DataAnnotation attributes, which works fine for client-side validation on the UI.
I then searched for implementation of server-side validation attribute for DataAnnotation where I found the [Remote] attribute that allow to call methods on the controller, looks pretty good.
I started with the following at the Controller:
public JsonResult IsValidName(string coin_name_2) { bool found = db.IGL_coin.Any(name => name.coin_name_2 == coin_name_2); if (!found) { return Json(true, JsonRequestBehavior.AllowGet); } return Json(false, JsonRequestBehavior.AllowGet); }
Then I decorate my model (IGL_coin) with MetaData class that holds the validation as follows:
// Metadata defenitionspublic class CoinMetadata { public int coin_id { get; set; } public string coin_name_1 { get; set; } [Required(ErrorMessageResourceType = typeof(Errors), ErrorMessageResourceName = "NameReq")] [Display(Name = "CoinName", ResourceType = typeof(ScreenRes))] [Remote("IsValidName", "Coin", ErrorMessageResourceType = typeof(Errors), ErrorMessageResourceName = "NameDuplicate")] public string coin_name_2 { get; set; } [Display(Name = "Rate", ResourceType = typeof(ScreenRes))] [Required(ErrorMessageResourceType = typeof(Errors), ErrorMessageResourceName = "RateReq")] [Range(0.00001,100000, ErrorMessageResourceType=typeof(Errors), ErrorMessageResourceName="RateInvalid")] public decimal coin_rate { get; set; } }// Partial Classes defenitions[MetadataType(typeof(CoinMetadata))]public partial class IGL_coin{}
Then my CoinCustomEditor partial view has the following markup to show the details:
<div style="margin: 5px 15px 5px 15px; height: auto; width: 350px"> @using (Html.BeginForm()) { @Html.AntiForgeryToken() @Html.HiddenFor(model => model.coin_id) @Html.ValidationSummary() <ul id="fieldlist"> <li> @Html.LabelFor(model => model.coin_name_2) @Html.TextBoxFor(model => model.coin_name_2) @*@Html.Kendo().MaskedTextBoxFor(model => model.coin_name_2).Name("coin_name_2").HtmlAttributes(new { style = "width: 100%;" })*@ @Html.ValidationMessageFor(model => model.coin_name_2) </li> <li> @Html.LabelFor(model => model.coin_rate) @Html.Kendo().TextBoxFor(model => model.coin_rate).Name("coin_rate").HtmlAttributes(new { style = "width: 100%;" }) @Html.ValidationMessageFor(model => model.coin_rate) </li> </ul> }</div>@Scripts.Render("~/bundles/jqueryval")
This partial view is called from the add/edit action on the grid using GridEditMode.PopUp and .TemplateName("CoinCustomEditor")).
The problem is that the remote attribute calls the method IsValidName correctly as shown by fiddler (see attached file).
The result in the Partial view shows the red border around the TextBoxFor but the error message is not shown.
Just in case this is the jqueryval scripts loaded by partial : @Scripts.Render("~/bundles/jqueryval"), which are defined at the _Layout as follows:
<head> <title>@ViewBag.Title - My App</title> @Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/kendo") @Styles.Render("~/Content/kendo/css") <link href="@Url.Content("~/Content/kendo/kendo.common.min.css")" rel="stylesheet" type="text/css" /> <link href="@Url.Content("~/Content/kendo/kendo.mobile.all.min.css")" rel="stylesheet" type="text/css" /> <link href="@Url.Content("~/Content/kendo/kendo.dataviz.min.css")" rel="stylesheet" type="text/css" /> <link href="@Url.Content("~/Content/kendo/kendo.default.min.css")" rel="stylesheet" type="text/css" /> <link href="@Url.Content("~/Content/kendo/kendo.dataviz.default.min.css")" rel="stylesheet" type="text/css" /> <link href="@Url.Content("~/Content/kendo/kendo.rtl.min.css")" rel="stylesheet" type="text/css" /> <script src="@Url.Content("~/Scripts/kendo/jquery.min.js")"></script> <script src="@Url.Content("~/Scripts/kendo/angular.min.js")"></script> <script src="@Url.Content("~/Scripts/kendo/jszip.min.js")"></script> <script src="@Url.Content("~/Scripts/kendo/kendo.all.min.js")"></script> <script src="@Url.Content("~/Scripts/kendo/kendo.aspnetmvc.min.js")"></script></head>
and also the BundleConfig definitions:
public class BundleConfig{ // For more information on bundling, visit http://go.microsoft.com/fwlink/?LinkId=301862 public static void RegisterBundles(BundleCollection bundles) { bundles.Add(new ScriptBundle("~/bundles/jquery").Include( "~/Scripts/jquery-{version}.js")); //bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include("~/Scripts/jquery.validate*")); bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include( "~/Scripts/jquery.validate.min.js", "~/Scripts/jquery.validate.unobtrusive.js")); // Use the development version of Modernizr to develop with and learn from. Then, when you're // ready for production, use the build tool at http://modernizr.com to pick only the tests you need. bundles.Add(new ScriptBundle("~/bundles/modernizr").Include( "~/Scripts/modernizr-*")); bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include( "~/Scripts/bootstrap.js", "~/Scripts/respond.js")); //bundles.Add(new StyleBundle("~/Content/css").Include( // "~/Content/bootstrap.css", // "~/Content/Site.css")); bundles.Add(new ScriptBundle("~/bundles/kendo").Include( "~/Scripts/kendo/2016.2.504/kendo.all.min.js", // "~/Scripts/kendo/kendo.timezones.min.js", // uncomment if using the Scheduler "~/Scripts/kendo/2016.2.504/kendo.aspnetmvc.min.js")); //bundles.Add(new StyleBundle("~/Content/kendo/css").Include( // "~/Content/kendo/2016.2.504/kendo.common-bootstrap.min.css", // "~/Content/kendo/2016.2.504/kendo.bootstrap.min.css")); bundles.IgnoreList.Clear(); bundles.Add(new StyleBundle("~/Content/kendo/css").Include( "~/Content/kendo/2016.2.504/kendo.common.min.css", "~/Content/kendo/2016.2.504/kendo.default.min.css")); }}
I hope to find some help, as server-side validation is crucial to go further.
Best
