ASP.Net Core MVC date input value - entity-framework

I created a edit view for a EF Model and have a problem with the datepicker.
Whenever i try to edit a `dataset, the edit view don't show me the old date values.
This is my Edit.cshtml Code:
<div class="form-group">
<label asp-for="BestellungsDatum" class="control-label"></label>
<input asp-for="BestellungsDatum" type="date" class="form- control" value="#Model.BestellungsDatum" />
<span asp-validation-for="BestellungsDatum" class="text-danger"></span>
</div>
This is what it looks like, but there have to be the old value at this position:
When i change the input to type="text", the value is shown but there is no datepicker.. Any solutions on this?

type="date" generates the browsers HTML-5 datepicker if supported. The specifications require that the value be formatted in ISO format (yyyy-MM-dd) which is then displayed in accordance with the browsers culture.
Add the asp-format to format the value, and remove the value attribute (Tag Helpers correctly generate the value attribute for correct 2-way model binding and you should never attempt to add it yourself)
<input asp-for="BestellungsDatum" type="date" asp-format="{0:yyyy-MM-dd}" class="form-control" />
Note the HTML-5 datepicker is not supported in IE, and only in recent versions of FireFox. If you want a consistent UI, then consider using a jQuery datepicker instead.

Just like to add an addition to the accepted answer:
<input asp-for="BestellungsDatum" type="date" class="form-control" />
Rather than adding asp-format to the view, add DataFormatString to the data Model or ViewModel, so you don't have to add it to every view that uses it:
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime BestellungsDatum { get; set; }

Related

Is it possible to not encode the Label tag value rendered in ASP.NET Core HtmlHelper?

I asked a similar question a while back:
Is it possible to format Display attribute's Name property value of MVC Model class?
I am wondering would it be possible to not apply encoding in the ASP.NET Core HtmlHelper.Label tag?
What I have now is:
<label asp-for="CorporationName" class="control-label"></label>
I understand it will render the html .
Like so:
<label class="control-label" for="CorporationName">Corporation / <br/>Entreprise</label>
with the text value defined in this model class:
[DataType(DataType.Text)]
[Display(Name = "Corporation / <br/> Enterprise")]
public string Corporation { get; set; }
So I am wondering if it is possible to apply something similar to #Html.Raw to the asp-for attribute value in the label tag above so it will show as:
Corporation /
Enterprise
Or I need to write my own label class so it will do the encoding?
Thank you.
I think your requirement can be known as, you want to use tag helper and display [Display(Name = "Corporation / Enterprise")] to display as Corporation / </br> Enterprise.
Basically, you are trying to change the html content appearance, and you want to realize it via taghelper, but actually there's no such feature, you may try to create a custom taghelper like this answer. But I think it's too complex.
In my humble opinion, you can modify the html content by js. But the easiest way to add linebreaker into html content is </br>, so my test code looks like below, it worked for me:
<div>
<label id="CorporationLabor" asp-for="Corporation" class="control-label"></label>
<input asp-for="Corporation" class="form-control" />
</div>
#section Scripts{
<script>
$(function(){
$("#CorporationLabor").html("Corporation / </br> Enterprise!!!");
});
</script>
}

How to capture change of Clarity Date Picker

I am using the double-binding of the clrDate, which allows me to load a Javascript Date object. However, I need to trigger an event every time the user changes the date, either types in a new one or uses the calendar.
I've tried (click), (change), (ngModelChange), (blur) - but for some reason none of them seem to trigger.
<label for="modelDate" class="clr-control-label" >Model Date:</label>
<input clrDate type="date" [(clrDate)]="selectedModelDate" (ngModelChange)="loadModel(false)" >
How should I capture the change within the Date Picker?
You can do it in two ways.
2-way Binding
In this stackblitz, notice that before the date picker is interaced with, if you click the button then the value of date1 is undefined. After you select a date and click the button it has the value set in the date picker. This is typical 2-way binding in Angular.
<section>
<button class="btn btn-primary" (click)="logDate1()">
Log Date Value for 2-way binding
</button>
<label for="date1" class="clr-control-label" > Date1:</label>
<input type="date" [(clrDate)]="date1" />
</section>
De-sugarized Syntax
This might be what you are after. It's almost the same as the above example but we seperate or de-sugarize the 2-way binding so that it gets set with [clrDate]=date2. Notice that it fires an event clrDateChange that can be tied into with the logChnage($event). That will get fired whenever the internal value of the date picker changes (e.g user selects a date) and you can do whatever you want with it's value in the logChange function. This is 2-way binding de-sugarized.
<section>
<h4>De-sugarized syntax</h4>
<label for="date2" class="clr-control-label" > Date1:</label>
<input type="date" [clrDate]="date2" (clrDateChange)="logChange($event)"/>
</section>

Convert data in TYPO3 fluid form

I need yours help. I have follow form:
{namespace femanager=In2code\Femanager\ViewHelpers}
<div class="femanager_fieldset femanager_membershipend control-group">
<label for="femanager_field_membershipend" class="control-label">
<f:translate key="tx_feusersplus_domain_model_user.membershipend" default="Expiration date of the membership"/>
</label>
<div class="controls">
<femanager:form.textfield
disabled="true"
id="femanager_field_membershipend"
property="membershipend"
class="input-block-level"
additionalAttributes="{femanager:Validation.FormValidationData(settings:settings,fieldName:'membershipend')}"
/>
</div>
</div>
And it get date in the next format - 1493942400. How I can make good look for date ? Thank you for advice!
Your property seems to be declared as integer (unix timestamp) in the model.
You can change the field to the following:
<femanager:form.textfield
disabled="true"
id="femanager_field_membershipend"
property="membershipend"
class="input-block-level" value="{yourModel.membershipend-> f:format.date(format: 'd.m.Y')}"
additionalAttributes="{femanager:Validation.FormValidationData(settings:settings,fieldName:'membershipend')}"
/>
This will format the property to a readable Date. But after that you will get problems saving the property correctly. If the property is really an integer, you need to write a converter for converting the date to an unix timestamp. It must work like here just in the reverse and I think TYPO3 does not have the correct converter for your needs out of the box.
I recommend you to change your property to DateTime (including the database field). Then you can use the DateTimeConverter of TYPO3 to save the data correctly.
EDIT: In your case the field is disabled="true" which should mean that the data will not become submitted. In that case you should not get problems with converting after submitting the form.

How do I link two input fields of type date?

I have two input fields:
<input name="check_in" type="date" class="checkin"> and
<input name="check_out" type="date" class="checkout"
I want to link them so when i pick a date from check_in field i can only pick the current day or a later day (earlier days disabled). After that I have to choose a day in the check_out field and i can only choose the day chosen in check_in or a later day (Other days have to be disabled)
I have been trying several solutions and code snippets but with no result
If you are using Bootstrap datepicker, then you can assign the start date of check_out to the date on check_in. Something like
$('.checkin').datepicker({
startDate: 'd' //this means today
});
$('.checkout').datepicker({
startDate: $('.checkin').val(); //Not sure of this but you get the idea
});
Alternative, just use the daterage option, which has everything implemented already:
<div class="input-group input-daterange">
<input type="text" class="form-control" value="2012-04-05">
<div class="input-group-addon">to</div>
<input type="text" class="form-control" value="2012-04-19">
</div>
And Javascript that goes with
$('.input-daterange input').each(function() {
$(this).datepicker('clearDates');
});
More here: https://bootstrap-datepicker.readthedocs.io/en/latest/markup.html#date-range
This will give you exactly what you need out of the box.

ASP.NET MVC 2 Input ID

Adding the following to my view:
<input type="text" runat="server" id="newBookingRef" name="newBookingRef" />
Results in the following HTML:
<input type="text" id="MainContent_newBookingRef" name="ctl00$MainContent$newBookingRef">
If I use the helper method Html.Textbox, the ID is generated as I would expect "newBookingRef".
How can I stop it prefixing the ID on standard input controls with the content placeholder id? I tried playing with the ClientIdMode of the content placeholder but this didn't seem to help.
Thanks,
Ben
Remove the runat="server" attribute.