Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Getting HiddenField value in GetValidators method of CustomModelValidatorProvider

Getting HiddenField value in GetValidators method of CustomModelValidatorProvider

Scheduled Pinned Locked Moved ASP.NET
htmlcssasp-netlinqdocker
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    indian143
    wrote on last edited by
    #1

    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
    
    E 1 Reply Last reply
    0
    • I indian143

      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
      
      E Offline
      E Offline
      Eric W Scott
      wrote on last edited by
      #2

      Are you sure that txtLookupTableId is being set correctly? You can use a tool like Fiddler[^] to inspect the raw HTTP post to check the value coming in.

      I 1 Reply Last reply
      0
      • E Eric W Scott

        Are you sure that txtLookupTableId is being set correctly? You can use a tool like Fiddler[^] to inspect the raw HTTP post to check the value coming in.

        I Offline
        I Offline
        indian143
        wrote on last edited by
        #3

        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."

        E 1 Reply Last reply
        0
        • I indian143

          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."

          E Offline
          E Offline
          Eric W Scott
          wrote on last edited by
          #4

          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!

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups