asp.net mvc - c# custom model binder for specific model and specific variable type -
I have a problem with myself in vain ints and validation text.
Actually I want to change the validation message when a null integer is not provided which is shown
from then
"value ' Xxxxxxxxxxxxxxxxxxxx "Invalid"
"Please provide a valid number"
I have a custom model the binder, such as
public class IntModelBinder: IModelBinder {public object BindModel (controllerContext controllerContext, ModelBindingContext bindingContext) {var integerValue = bindingContext.ValueProvider.GetValue (bindingContext.ModelName); If (integer value == tap = integer value. Used value == "") empty return; Var integer = integer value. Used value; Bindingkntestkmodelstteksetmodelvlua (Bindingkntestkmodelnme, Bindingkntestkvluaproviderkjtwlua (Bindingkntestkmodelnme)); Try {return int.Parse (integer); } Catch (exception) {bindingContext.ModelState.AddModelError (bindingContext.ModelName, String.Format ( "\" {0} \ bindingContext.ModelName "is invalid, enter a valid number.")); Return tap; }}}
At this time I have updated my global.asax.cs, so all tap inbelt uses this model binder, although I have all these tapable inputs I just do not want to use it, I just want a specific model to use it and only use my model binder on Nullible Ents in that model. Is there any way that I can tie this model bander in my model and collaborate only with faucetable variable variables?
I've tried to use his Modbandr on its models like
[ModelBinder (typeof (IntModelBinder))] public class CreateQuoteModel {....} < / Code>
but it does not check on inert ints, I want to avoid the third party addon
If you are returning empty on nullable ints are
if (integerValue == null || integer value. Try the value == "") empty return made;
So that you are not adding that error to your inability to the nullible ints.
Apart from this, I recommend you to
int result = 0; If (! (Integer, out result)). (Baiding Contaks. Modelstet. Ademodel Arrr (binding Kantboks. Madelnam, Stringkformet ( "\" {0} \ "is invalid, please enter a valid number." Binding Kantboks. Modelnam)); return null;} return Results;
Comments
Post a Comment