I have an MVC3 form (C#) that is tied to a Csla 4.2 business object. I have a "scheduleddate" property that needs to be blank when the form is created, and then filled in by someone else when they schedule the activity. The problem I have is that after a form is created, it shows up in the view/edit screen with a value of 1/1/1900, and I want it to show up blank.
I used to do this in earlier versions of Csla and VB by having a SmartDate variable and a string property that returned variableX.text, which would be an empty string if null in the database.
I have tried using the SmartDate and DateTime, but still have the same issue. Here is the property from the Csla object:
public static readonly PropertyInfo<DateTime> DateScheduledProperty = RegisterProperty<DateTime>(c => c.DateScheduled);
[Display(Name = "Date Scheduled")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime DateScheduled
{
get { return GetProperty(DateScheduledProperty); }
set { SetProperty(DateScheduledProperty, value); }
}
and here is my MVC code in the view:
@Html.EditorFor(model => model.DateScheduled
Any help would be greatly appreciated.
To use SmartDate you need to implement a SmartDate field with a string property and use GetPropertyConvert and SetPropertyConvert.
If your property type is DateTime then the UI will never get a blank value, because the value must always be some valid date/time.
Alternately, you can write code in your controller or view to convert an "empty" date to an empty string for display to the user.
Thank you, this part is working now. If I don't want it to write 1/1/1900 to the database when the value is blank, should I just check for it in the controller, or is there something I can do in the property?
public static readonly PropertyInfo<SmartDate> DateScheduledProperty = RegisterProperty<SmartDate>(c => c.DateScheduled);
[Display(Name = "Date Scheduled")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public string DateScheduled
{
get { return GetPropertyConvert<SmartDate, string>(DateScheduledProperty); }
set { SetPropertyConvert<SmartDate, string>(DateScheduledProperty, value); }
}
SmartDate has a DbValue property that returns dbnull (I think) when the date is empty.
I should have mentioned that I am making a jump from Csla version 3.7 and VB to Csla 4.2 and C#. There's probably some syntax issue that I am missing. I used to be able to do this in the old Csla/VB world where the parameter is passed to a stored procedure:
cmd.Parameters.AddWithValue("@ScheduledDate", ScheduledDate.DBValue)
If I try to do the same thing in Csla 4.2 and C#, intelisense does not show DBValue as an option.
Thank you for your patience and assistance.
Well the ScheduledDate property is of type string right?
You need to access the underlying field value itself. You can do this with the ReadProperty method, because the field manager sees the field/property as being a SmartDate - it is the property itself that uses the convert helpers to make it a string value.
Dim temp = ReadProperty(ScheduledDateProperty)
cmd.Parameters.AddWithValue("@ScheduledDate", temp.DBValue)
Understood, this makes sense and works like I need it to. Thank you for the information!
Copyright (c) Marimer LLC