Thank you very much for your time
My question is How to set value input of type date?
@Html.EditorFor(model => model.StartDate, new { htmlAttributes = new { @class = "form-control", @type = "date" } })
that it comes after Run
<input class="form-control text-box single-line" data-val="true" data-val-date="The field Date must be a date." data-val-required="The Date field is required." id="StartDate" name="StartDate" type="date" value="">
I made an some code in this manner
$(document).ready(function () {
debugger;
var date = $('#StartDate').val();
var dt = new Date(date);
var day = parseInt(dt.getDate());
var month = parseInt(dt.getMonth() + 1);
var year = parseInt(dt.getFullYear());
if (day < 10) { day = '0' + day }
if (month < 10) { month = '0' + month }
var setDate = year + '-' + month + '-' + day;
$('#StartDate').val(setDate);
});
When the program runs I see the value from the debugger screen is string.empty
var date = "";
also I tried to remove this one the lower portion of format error again.
var dt = new Date(date);
but I didnt work. if anyone knows the answer to this I really need help Finally Coming data in view but value does not show because of format is different. This required format(yyyy-MM-dd)
Thank you to everyone.
@Html.TextBoxFor?? your rendered html hasvalue=""empty.. so even in your script it is emptyStartDatein the controller before you pass the model to the view. And if you want to format it, Use theDisplayFormatAttribute(orTextBoxFor(m => m.StartDate, "{0:yyyy-MM-dd}", new { ... }))new { @class = "form-control", @type = "date" }if you wanttype="date"(I just kept the code in my comment to the minimum). The key point was that you script is pointless. You need to set the value in the controller.