Wednesday, 15 September 2010

c# - unable to get property value of undefined or null reference -



c# - unable to get property value of undefined or null reference -

i using below code validate entered delivery date should not less today date .. purpose have done below ....

i using custom validation using jquery ...

this model:

[uihint("date")] [deliverydatecheck] [displayformat(dataformatstring = "{0:mm/dd/yyyy}", applyformatineditmode = true)] [required(errormessage="required")] public datetime? deliverydate { set; get; }

and server side code validation

public class deliverydatecheck : validationattribute, iclientvalidatable { protected override validationresult isvalid(object value, validationcontext validationcontext) { string errormessage = "delivery date should today date or greater"; if(value != null) { datetime dt = (datetime)value; if (dt.date < datetime.now.date) { homecoming new validationresult(errormessage); } } homecoming validationresult.success; } public ienumerable<modelclientvalidationrule> getclientvalidationrules(modelmetadata metadata, controllercontext context) { modelclientvalidationrule mcvrule = new modelclientvalidationrule(); mcvrule.validationtype = "checkdate"; mcvrule.errormessage = "delivery date should today date or greater"; yield homecoming mcvrule; } }

and client side validation

$(document).ready(function () { jquery.validator.unobtrusive.adapters.add('checkdate', {}, function (options) { options.rules['checkdate'] = true; options.messages['checkdate'] = options.message; }); jquery.validator.addmethod('checkdate', function (value, element, params) { if (value) { var todaydate = new date(); var comparedate = value.date; if (comparedate < todaydate) { homecoming false; } } homecoming true; }); });

but getting error ...

error

0x800a138f - javascript runtime error: unable property 'value' of undefined or null reference

i can see server side validation working fine not able client side validation 1 has thought why getting error @ here.....

many in advance

shouldn't comparedate follows:

var comparedate = new date(value);

c# javascript jquery asp.net-mvc validation

No comments:

Post a Comment