Error handling when jqGrid converts data of the null date type

Source: Internet
Author: User

Use jqGrid to display database tables. If the database table has a date type, you must declare the field type that can be empty in poco. See the following FillinDate field:

Public class Project2Weekly

{
Public int Id {get; set ;}
Public string Progress {get; set ;}
Public string Problem {get; set ;}
Public string NextPlan {get; set ;}
Public string Evidence {get; set ;}
Public {get; set ;}
Public string FillinMan {get; set ;}
Public int Pid {get; set ;}
}

After editing and saving table data, you must call the JqGrid. EditParams method to convert the data on the Form, for example:

Public JsonResult EditWeekly ()
{
NameValueCollection nvc = Request. Form;
Project2Weekly model = <Project2Weekly> (nvc );
Manager. EditWeekly (model );
Return Json (new {success = true });
}

In practice, we found that the date type on Form is always not saved in the database. Tracking found that DateTime in poco? The data type is in JqGrid. the EditParams method is not recognized as the DateTime type, but is recognized as "Nullable '1" (System. nullable '1 [System. dateTime]), causing the value to be set to null. The source code of the JqGrid. EditParams method is as follows:

 

Public static T EditParams <T> (NameValueCollection nvc)

{

Type type = typeof (T );

PropertyInfo [] pis = type. GetProperties ();

 

T t = (T) Activator. CreateInstance (type );

 

Foreach (PropertyInfo pi in pis)

{

String data = nvc [pi. Name] = null? "": Nvc [pi. Name]. ToString (). Trim ();

Dynamic value;

Switch (pi. PropertyType. Name)

{

If (data = "")

Value = DateTime. MinValue;

Else

{

Value = Convert. ToDateTime (data );

If (value = DateTime. MinValue)

Value = DateTime. Now;

}

Break;

.

.

.

Case "String ":

Value = data;

Break;

Break;

}

 

Pi. SetValue (t, value, null );

}

 

Return t;

}

}

 

The solution to this problem is also very simple. Add a line in the case "DateTime" code to process the "Nullable '1" type. The code is modified as follows:

 

Switch (pi. PropertyType. Name)

{

Case "DateTime ":

 

If (data = "")

Value = DateTime. MinValue;

Else

{

Value = Convert. ToDateTime (data );

If (value = DateTime. MinValue)

Value = DateTime. Now;

}

Break;

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.