Sensenet DateTime Field Default Value - sensenet

When DateTime field is left blank, Sensenet displays (system default) '01/01/01 12:00' in the content browse mode. Can it display no value since it was not entered by users?

You can also solve this with server side code in your content handler.
If you instantiate a new DateTime object its default value is 1/1/0001 12:00:00 AM, also specified as DateTime.MinValue. DateTime.MaxValue is 12/31/9999 11:59:59 PM. You then test for DateTime.MinValue to address formatting.
If you have content where you will be formatting and displaying dates a lot, it is often simpler in your Content Type Definition (CTD) to define a string field that corresponds to the date field. For example, your CTD could have fields like this:
<Field name="ReviewDate" type="DateTime" >
<DisplayName>Review Date</DisplayName>
</Field>
<Field name="ReviewDateStr" type="ShortText" >
<DisplayName>Review Date</DisplayName>
</Field>
Then in your content handler, you create a read only getter to display the ReviewDate:
private const string REVIEWDATESTRPROPERTY = "ReviewDateStr";
[RepositoryProperty(REVIEWDATESTRPROPERTY, RepositoryDataType.String)]
public virtual string ReviewDateStr
{
get
{
if (ReviewDate == DateTime.MinValue)
{
return "n/a"; // Default string if date is not set.
}
return ReviewDate.ToString(); // Add date formatting here.
}
}
Alternatively, you can create a field control that does that same thing.

DateTime field's default value cannot be blank, so if you want to hide it in a Browse view you can use a script to check datetime values and if it contains 01/01/01 you can simply remove/hide it with js in the browser. There's a helper function to format dates (SN.Util.js 'setFriendlyLocalDate') which handles this issue with the default value too.

Related

Razor form for input -- how to display a template for input?

How do I create a template four user input? The requirement is to have an input text box display something line '0000-0000-00" to get a 10 digit number from a user that will be stored in the data base with out the '-'. This is for the aspnet core 3.1 Razor page.
In the view you add a placeholder to the input tag:
<input asp-for="MyProperty" placeholder="0000-0000-00"/>
You decorate the property MyProperty with an regex data annotation:
[RegularExpression("^([0-9]{4})-([0-9]{4})-([0-9]{1})$", ErrorMessage = "Invalid input")]
public string MyProperty{ get; set; }

Razor Pages set current date on page load

I'm new to razor and .net, i have a page on an ASP Net Razor project where i popup a modal with a form inside. Inside the form there is a datepicker and i want to set the system date when the page loads and evenly keep it editable.
i've already tried with
#{ string dateNow = DateTime.Now.ToString("dd MM yyyy"); }
<input asp-for="intervento.Data" class="form-control" type="date" value="#dateNow" />
it retrieves the correct date but can't display it. how can i solve this?
Your date should be in the following format yyyy-MM-dd.
So it would look like this:
<input type="date" value="2020-10-23">
Presumably, Intervento.Data is a DateTime property of your PageModel? If so, you should add a DataType attribute to specify that it represents a date, not a time:
[DataType(DataType.Date)]
public DateTime Data { get; set; }
Then you don't need to specify the type on the input. The tag helper will generate the correct value based on the attribute. Also, if you set a default value on the property:
[DataType(DataType.Date)]
public DateTime Data { get; set; } = DateTime.Now;
you won't need to set the value of the input either. ASP.NET will take care of formatting the value correctly to an ISO 8601 format that the input can work with. So the following will suffice:
<input asp-for="intervento.Data" class="form-control" />

How to make a field readonly if the field store a value odoo 12?

I tried like this given below but its not getting what i wanted,what im trying to aquire is if that field(Char) is updated(stored a value) by model B it needs to be readonly and else by other model fields needs to be not readonly :
<field name = 'name' attrs="{'readonly': [('name','=', True)]}"/>
To check if a char field is not set in attrs, compare it to False, you can find an example in the account partner view.
Example:
<field name='name' attrs="{'readonly': [('name','!=', False)]}"/>
If we use the above definition and try to set the field value, the field value will not be saved because the field is read-only, to avoid that we need to set the force_save attribute to True to force Odoo to save the value on the read-only field.
Example:
<field name='name' attrs="{'readonly': [('name','!=', False)]}" force_save='True'/>

Using input time in Blazor server side has strange behavior

Out side an EditForm I want to use a input type="time" as the following code shows:
<input type="time" class="form-control" id="sTime" #bind="#StartTime">
the Bound Property should be a DateTime variable otherwise it does not allow, As this code shows:
DateTime StartTime { get; set; } = DateTime.Now;
private async Task AddingVisit()
{
...
VisitStart = startDate.Date + TimeSpan.Parse(StartTime.ToShortTimeString());
...
}
But when the code in the method reaches to the point that uses StartTime property, It jumps out from code to HTML and begins doing some works and then does not come back to execute the remaining code, Why this happens and how to solve this?
Strangely, Removing TimeSpan.Parse Solved the problem:
DateTime VisitStart = startDate.Date + StartTime.Value.TimeOfDay;
The execution now is normal, and does not jump to Html.

MVC view displaying time when date only required

I have the following model property
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:d/M/yyyy}", ApplyFormatInEditMode = true)]
[Display(Name = "Date of Screening")]
public DateTime? ScreeningDate { get; set; }
and the following view:
#Html.TextBoxFor(model => model.ScreeningDate, new { #class="date maintainState" })
and yet I get this markup for the textbox:
<input value="18/06/2013 12:00:00 AM" class="date maintainState" ... type="text" />
I want the value to be 18/06/2013
It works when I apply #Html.EditorFor, but I need control over the class attribute. What am I doing wrong? Thank you very much.
It works when I apply #Html.EditorFor, but I need control over the
class attribute. What am I doing wrong?
Nothing, it's how things work. Only the EditorFor and DisplayFor helpers respect the [DisplayFormat] attribute.
I understand that you are using TextBoxFor because you want to apply a custom class to the field which is what the EditorFor doesn't allow you to. But that's not true. All that the EditorFor helper does is to render the corresponding editor template. And if you do not have a custom template it renders the default one.
So what?
Well, you write a custom editor template for the DateTime type that will behave as you want. Just like that in ~/Views/Shared/EditorTemplates/DateTime.cshtml:
#Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, ViewData)
And then simply:
#Html.EditorFor(model => model.ScreeningDate, new { #class="date maintainState" })
Now killed 2 rabbits with one bullet:
We applied the desired format
We applied the desired class to the input field
And if you wanna kill a third rabbit and be able to validate this custom format when the form is submitted back (because remember that the default model binder doesn't care much about this [DisplayFormat] attribute) you could write a custom model binder that will respect the [DisplayFormat] attribute, just as I explained here: https://stackoverflow.com/a/7836093/29407