MVC :: Unable To Get DataAnnotations Client Validation And Nullable DateTime Property

May 12, 2010

I am having a problem with the Client Validation not firing when I have marked a nullable DateTime field as [Required] and the value of the field is null. (It is actually stopping ALL client validation on the page working - not just the DateTime field).

Interestingly enough, once a post back has been done and the error picked up on the server, the client validation starts working.

Also if you set the date to a none null value (see commented out line in the controller) it will also work

I have replicated this behaviour in a small test project (this is deliberately very Noddy - just enough to replicate the problem)

Am I doing something fundamentally wrong or am I looking at coding some sort of work around here? (I realise that a Required nullable field sounds like a bit of a oxymoron, but I wanted to present the user with a blank field which they must then fill, not one pre-populated with an arbitrary date value)



View (Auto generated)



View 5 Replies

Similar Messages:

DataAnnotations On A DateTime Property?

Jul 15, 2010

I have a DateTime property in my model and I would like to validate it using DataAnnotations.In my view I want to get that DateTime as a number of days (e.g. "3"), instead of a DateTime (e.g. "7/14/2010"). If user enters "3" in the view, then DataAnnotation finds it invalid -and that is the issue.What are my options in this scenario?

View 2 Replies

MVC :: 2 RC On 3.5 SP1 - Client Side Validation From DataAnnotations?

Jan 19, 2010

I have tried everything to get client-side validation to work in MVC2 RC, but have been completely unsuccessful. Server-side validation works just fine, but client-side never fires. I have grabbed


from the futures source, since it wasn't included in RC. Unfortunately, that's the only script that people agree is necessary for everything to work. Besides that, nobody seems to know which other specific js scripts are needed. The candidates are (jquery-1.3.2.js, jquery.validate.js, MicrosoftMvcAjax.js, MicrosoftMvcJQueryValidation.js). I've tried every permutation of these and other scripts to no avail.

Html.EnableClientValidation() has been included before BeginForm, and the JSON validation data appears to be correctly written into the source of the page. But, again, client-side validation never fires.

View 35 Replies

MVC :: Nullable DateTime And DatePicker - Dictionary Requires A Non-null Model Item Of Type 'System.DateTime

Apr 1, 2011

I have a problem:

I've created usual Controller and View(Edit view) for editing my Entity (EntityFramework)

Here is view example:

<div class="editor-field">

@Html.EditorFor(model => model.BirthDate)

@Html.ValidationMessageFor(model => model.BirthDate)


BirthDate is Nullable<DateTime>

but during loading my View I get this exception

The model item passed into the dictionary is null, but this dictionary requires a non-null model item of type 'System.DateTime'.

That's because of BirthDate is null in database but it is nullable and I expect that it just leaves the filed empty.

And I have Shared EditorTemplate:

@model System.Nullable<System.DateTime>

@if (Model.HasValue)

View 2 Replies

MVC :: Client Side Validation For Property Classes?

Feb 8, 2011

I have a model class which consists of some other classes as its properties.The name class is named Person and has an Address property which is another class and has certain required properties such as City or Line1.The 'required' constrain for 'City' and 'Line1' is properly observed by the server-side validation by the client-side validation seems to be ignoring them.I have properly called Html.EnableClientValidation and have all the required scripts in place.Is there a way to force client-side validation for the described scenario or I'm gonna have to flatten the model class?

View 7 Replies

MVC :: When Enable Client Validation The Property Always Gets Flagged As Invalid?

Jun 2, 2010

I have a property like this on my view model:


The date range validates perfectly during model binding. However, . Looking at the client validation code in MicrosoftMvcValidation.js, it appears that the Sys_Mvc_RangeValidator can only handle numeric ranges

View 1 Replies

C# - Comparing Nullable DateTime's In

Nov 17, 2010

I am a c#/ developer and I am having to work on a VB/ I started out with VB.NET but after years away from it I'm getting confused with the syntax.

I have two variables

Dim originalDate as DateTime?
Dim newDate as DateTime?

Both nullable datetimes, originalDate is a nullable date I am getting from the database and newDate time is set in code, I need to compare them, they can either both have dates, neither have dates or one have and one not.

I have a bit of code as follows:

if origEndDate = origEndDate then

When both origEndDate and origEndDate are "nothing" this statement is false (well when I run it in the watch window it comes back as nothing)!

I don't understand why this is the case because I was under the impression doing an "=" compares the two values and as they are the same surely it should be true?

What syntax should I be using as in C# I can do the above as so:

if (origEndDate == origEndDate) { }

and it will come back as true.

View 4 Replies

Pass Parameter To Nullable DateTime Field

Dec 23, 2015

I have a textbox that I use to gather a date.  I would like to have the database store a null if this field is left blank by the user when submitting. I am using the following on my aspx page:

<div class="col-md-3">
<strong>Date Submitted to TDI</strong><br />
<asp:TextBox ID="sub_tdi_dt" runat="server" Enabled="true"></asp:TextBox>
<asp:ImageButton ID="sub_tdi_dt_cal_popup" Enabled="false" ImageUrl="Images/calendar.png" ImageAlign="Bottom" runat="server" />
<ajaxToolkit:CalendarExtender runat="server" BehaviorID="sub_tdi_dt_CalendarExtender" PopupButtonID="sub_tdi_dt_cal_popup" TargetControlID="sub_tdi_dt" ID="sub_tdi_dt_CalendarExtender"></ajaxToolkit:CalendarExtender> </div>

I have tried using the following in Page_Load, but it doesn't work - I still get '1/1/1900':

DateTime? sub_tdi_dt = null;

View 1 Replies

Nullable DateTime Parameter Never Bound When Calling Action

Sep 20, 2010

I have the following function signuture:

public JsonResult PopulateGrid(int page, Guid? accountId, Guid? systemUserId, Guid? branchId, DateTime? fromDate, DateTime? toDate, HomeVisitType? homeVisitType)

Every single parameter is bound just fine except toDate which turns out to be always null. When inspecting the Request.QueryString["toDate"] it retrives the right value which is 30/09/2010. It seems that DateTime expects another format when binding.
What is the right format?

View 1 Replies

MVC :: Nullable DateTime Give Invalid Modelstate If Null?

Mar 19, 2011

Got a nullable datetime member on my viewmodel.the moment it hit my controller action, the viewmodel's modelstate allready are invalid (because the member is null) that works? (using MVC2)Basically my viewmodel have a social security number, passport number and passport expiracy date.If the ssn is entered, I want to ignore the two passport related fields, so I made the expiracy date member nullable.

Like all other viewmodels in this app, each model have it's own validation function that will do validation, and in there I add an error that the date is required or not valid (if using passport number). mvc itself should not care about the date at all.I dont want to give the date a default value, because then the date will show when the form loads, and it must be an empty field (unless I already got a date of course.

View 4 Replies

DataSource Controls :: Linq OrderBy With Nullable DateTime?

May 3, 2010

Is there a way to use the OrderBy command in Linq with a DateTime field using the Date portion only (without the Time Stamp portion) that is nullable such as in the following query?

var query1 = from myTable in MyTable
where myTable.EnteredDate != null
group myTable by myTable.EnteredDate into myOutput[code]....

Since myTable.EnteredDate is a DateTime field that can contain nulls, it does not support the .Date conversion call such as

Is there a way to convert the myTable.EnteredDate field into a date only string in the group by portion?

View 3 Replies

DataSource Controls :: Query A Nullable System.DateTime Column?

Apr 23, 2010

Am I going to be able to query (using LINQ to SQL) a database field with a type like the following "public System.Nullable<System.DateTime> Spouse_DOB"?

I'm trying to use the following code: [Code]....

and I get the error "'System.Nullable<System.DateTime>' does not contain a definition for 'Month' and no extension method 'Month' accepting a first argument of type 'System.Nullable<System.DateTime>' could be found (are you missing a using directive or an assembly reference?)"

Is it because this DateTime column allows a NULL value (since everyone isn't married in my database)?

Error 1 'System.Nullable<System.DateTime>' does not contain a definition for 'Month' and no extension method 'Month' accepting a first argument of type 'System.Nullable<System.DateTime>' could be found (are you missing a using directive or an assembly reference?)
C:Working WebsitesGrannys Clan Address BookDefault.aspx.cs
38 26
C:Working WebsitesGrannys Clan Addres

View 1 Replies

Disable DataAnnotations Validation On DefaultModelBinder?

Mar 16, 2010

I want DefaultModelBinder not to perform any validation based on DataAnnotations metadata. I'm already using DataAnnotations with DynamicData for the admin area of my site, and I need a different set of validation rules for the MVC based front-end. I'm decorating my classes with the MetadataType attribute. If I could have different MetadataType classes for the same model but used on different scenarios that would be great.If not I'm fine with just disabling the validation on the DefaultModelBinder, either by setting some property or by creating a specialized version of it.

View 1 Replies

MVC :: DataAnnotations Validation Suppose To Validate Like This?

Jan 30, 2011

I got a Category model and it looks like this


My problem is that when i do a tryupdatemodel it validate the ParentCategory aswell, is it really suppose to do that? How could i avoid this, I tried [Bind(Excluede = "ParentCategory)] in the ActionResult but with no luck. I have looked all over the place with no luck everybody just got the Id problem.

View 1 Replies

MVC :: Custom Validation Against Repository With Dataannotations

Jan 11, 2010

I'm having problems with getting my custom dataannotations to work, I'm trying to add a validation-attribute that validates that the UsergroupName for a Customer (CustomerID) is unique.


the IsValid should return false if the "count >0". How do I fix this one so it works. GetUsergroups() returns IQueryable<Usergroup>.

View 8 Replies

Web Forms :: UserControl With DateTime Property - Set Markup In Local Datetime Format

Jan 21, 2010

I created a UserControl and added a public DateTime property. I want to set this property in Markup (ie. <cc1:MyControl runat="server" id="MyControl1" DateTimeProp="1/1/2010" /> ) however I live in a place with a dd/mm/yyyy date format so it complains if I set something like "20/1/2010". Is there a way to set markup in my local datetime format or do I have to conform to "mm/dd/yyyy"?

View 2 Replies

MVC :: Using DataAnnotations As Well As Server-side Validation Logic?

Apr 6, 2010

I'm using DataAnnotations as well as server-side validation logic.

Is there a way to show DataAnnotation's client-side validation errors in the ValidationSummary, as it shows when returning from server validation?

I know I can use ValidateMessage and ValidateMessageFor next to elements, but the spacial design of the form I'm working with doesn't allow for anything other than highlighting the HTML element, however, I would still like to have a message saying what's wrong.

If I place ValidateFor's alongside ValidationSummary in order to mimic a ValidationSummary during client-side validation, they both appear when returning from a failed server side validation.

View 4 Replies

C# - Custom Control With Nullable Property That Has A Default Value?

Feb 10, 2011

I can create a custom control with a default value:

private bool exclue = false;
public bool Exclude { get { return exclue; } set { exclue = value; } }

I can Create the same thing with a nullable property:

private EntityStatuses? status = EntityStatuses.Active;
public EntityStatuses? Status { get { return status; } set { status = value; } }

But how can i then set the property to null in markup when using the custom control?

<MyControls:Control ID="Con" runat="server" Status="?" >

View 3 Replies

C# - Unable To Set A Nullable Int To Null In A Ternary If Statement?

May 4, 2010

The C# code below:

int? i;
i = (true ? null : 0);

gives me the error:

Type of conditional expression cannot be determined because there is no implicit conversion between '<null>' and 'int' Shouldn't this be valid?

View 3 Replies

How To Write Extension Methods For Both Nullable And Not Nullable

Nov 10, 2010

I've written the following Extension Method

Public Function ToUtcIso8601(ByVal dt As Date) As String
Return String.Format("{0:s}Z", dt)
End Function

But I also need a Nullable version of the same method... how exactly do I do this?

This is what I was thinking, but I'm not sure if this is the right way


View 1 Replies

MVC :: Validation Parameter Names In Unobtrusive Client Validation Rules Must Start With A Lowerc?

Oct 7, 2010

I am migrating my application to MVC3 and getting this error message Validation parameter names in unobtrusive client validation rules must start with a lowercase letter and consist of only lowercase letters or digits.How can I ovoid it without going and modifying all my ViewModels

View 10 Replies

MVC :: Register Client-side Validation For Custom Validation Attribute?

May 19, 2010

I am trying to create a custom password validation attribute which has these requirements

1. cannot be empty.

2. doesn't contain space or other special chracters, which I'll define later.

3. length is between 6 - 12


it works fine on the server-side but I'd like to make this work on the client side as well, so I created a PasswordAttributeAdapter class


and in global.asax I added


View 6 Replies

C# - Show Only Client-side Validation With Client And Server Validators?

Dec 12, 2010

If I have something like the following:

<asp:TextBox id="test" runat="sever"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ControlToValidate="test"
<asp:Label runat="server" id="lblStatus"></asp:Label>

if in the code behind, I set the label to "You must enter a value if the textbox is empty" to validate it on the server side and this is fine, but if javascript is enabled, I don't want to show the client side validation and the server side label together. Is this something where I would just have to disable the client-side validators if javascript was enabled?

View 3 Replies

Validation: Gathering Information From Client-side Validation?

Jan 18, 2011

I'd like to implement ajax message box that will gather information from validators on a page when client-side validation fires. Like: I have left some text fields blank then press submit button and appropriate RequiredFieldValidators show messages. I would like my message box shows those messages too. I am looking for appropriate way to hook on validators JS event or something.

View 2 Replies

MVC :: Create Client Validation Automatically When Use Own Validation Class?

Mar 2, 2011

I write a class inherited from ValidationAttribute such as

public class MyValidation:ValidationAttribute

but when I use MyValidation,and EnableClientValidation,I found that it would not create client script automatically .

View 5 Replies

Copyrights 2005-15, All rights reserved