Getting HiddenField value in GetValidators method of CustomModelValidatorProvider
-
Hi all, I have overridden GetValidators method of CustomModelValidatorProvider in class CustomModelValidatorProvider, when a Post is happening, this Validator is getting all the values that I have input on the screen but I have Hidden field that I want to pass into this method is not coming in.
public class CustomModelValidatorProvider : System.Web.Mvc.DataAnnotationsModelValidatorProvider { protected override IEnumerable GetValidators(System.Web.Mvc.ModelMetadata metadata, ControllerContext context, IEnumerable attributes) { var z = metadata.PropertyName; if ((metadata.PropertyName == "Code") && (metadata.ContainerType.FullName == "DHCS.BH.Provider.Models.LookupTable")) { LookupTable model = metadata.Container as LookupTable; var newAttributes = new List(attributes); var stringLength = newAttributes.OfType().FirstOrDefault(); if (stringLength != null) { newAttributes.Remove(stringLength); if (model.CodeLength != 0) { newAttributes.Add(new StringLengthAttribute(model.CodeLength) { MinimumLength = model.CodeLength, ErrorMessage = @"The field {{0}} length must be at least {model.CodeLength}." }); } attributes = newAttributes; } } return base.GetValidators(metadata, context, attributes); } public string GetPropertyName(Expression\> propertyLambda) { var me = propertyLambda.Body as MemberExpression; if (me == null) { throw new ArgumentException("You must pass a lambda of the form: '() => Class.Property' or '() => object.Property'"); } return me.Member.Name; } }
My cshtml file is as below, it is a Popup that's being generated for Create and Edit buttons of Kendo Grid.
@model DHCS.BH.Provider.Models.LookupTable
@{
ViewBag.Title = "EditLookup";
}@using (Html.BeginForm())
{
@Html.AntiForgeryToken()@\* @Html.Kendo().TextBoxFor
-
Hi all, I have overridden GetValidators method of CustomModelValidatorProvider in class CustomModelValidatorProvider, when a Post is happening, this Validator is getting all the values that I have input on the screen but I have Hidden field that I want to pass into this method is not coming in.
public class CustomModelValidatorProvider : System.Web.Mvc.DataAnnotationsModelValidatorProvider { protected override IEnumerable GetValidators(System.Web.Mvc.ModelMetadata metadata, ControllerContext context, IEnumerable attributes) { var z = metadata.PropertyName; if ((metadata.PropertyName == "Code") && (metadata.ContainerType.FullName == "DHCS.BH.Provider.Models.LookupTable")) { LookupTable model = metadata.Container as LookupTable; var newAttributes = new List(attributes); var stringLength = newAttributes.OfType().FirstOrDefault(); if (stringLength != null) { newAttributes.Remove(stringLength); if (model.CodeLength != 0) { newAttributes.Add(new StringLengthAttribute(model.CodeLength) { MinimumLength = model.CodeLength, ErrorMessage = @"The field {{0}} length must be at least {model.CodeLength}." }); } attributes = newAttributes; } } return base.GetValidators(metadata, context, attributes); } public string GetPropertyName(Expression\> propertyLambda) { var me = propertyLambda.Body as MemberExpression; if (me == null) { throw new ArgumentException("You must pass a lambda of the form: '() => Class.Property' or '() => object.Property'"); } return me.Member.Name; } }
My cshtml file is as below, it is a Popup that's being generated for Create and Edit buttons of Kendo Grid.
@model DHCS.BH.Provider.Models.LookupTable
@{
ViewBag.Title = "EditLookup";
}@using (Html.BeginForm())
{
@Html.AntiForgeryToken()@\* @Html.Kendo().TextBoxFor
-
Yes I mean in the JavaScript function but I am not sure if something else is happening between the RequestEnd javascript function call and opening the Popup. I want to write a text changed event for the Kendo Textbox, idTxtForCodeLookupCode, can you please help me how to write it using jquery, maybe if I assign value to the HiddenField: txtLookupTableId, it would be good. Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."
-
Yes I mean in the JavaScript function but I am not sure if something else is happening between the RequestEnd javascript function call and opening the Popup. I want to write a text changed event for the Kendo Textbox, idTxtForCodeLookupCode, can you please help me how to write it using jquery, maybe if I assign value to the HiddenField: txtLookupTableId, it would be good. Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."
With this code:
@Html.HiddenFor(model => model.LookupTableId, new { id = "txtLookupTableId" })
You are setting the value of the hidden field to whatever is in your model's LookupTableId property. This is done on the server side when the view is rendered. I don't know anything about Kendo components so I can't help you there. For a troubleshooting approach on the client side, try using the developer tools built in to the browser (F12 on chrome). You can set breakpoints and log to the console using console.log. This should help you see what is going on in your event handler. Good luck!