I am doing my first ASP.net MVC application and I am facing some problems that I do not know how to fix. I have been trying to find answers from here as well but no luck so far, so I really hope you can help me.
So what I need my application to do is once I select an start date and end date I will click the button and it will bring back the data from selected period. I can choose the dates but for some reason the button dos not work, I can not click it. Debuging gives me an error saying:
SCRIPT5022: InvalidStateError
I assume that the problem is in here, but I am not sure:
var startdate = $("#startdate").datepicker('getDate');
I tried to test the button by writing it like that:
var startdate = "";
and it takes me to controller, but if it has an value its not working. The code that I have in MVC view is following:
@model Proovitoo_v4.Models.DatePickerModel
@{
ViewBag.Title = "Index";
}
@using (Html.BeginForm())
{
<label>Start date</label><br />
@Html.EditorFor(model => model.StartDate, new { htmlAttributes = new { @class = "datepicker"@*, @required = "true", type = "date", value = "startdate"*@ } })
<br />
<label>End date</label><br />
@Html.EditorFor(model => model.EndDate, new { htmlAttributes = new { @class = "datepicker"@*, @required = "true", type = "date", value = "enddate"*@ } })
}
<br />
<br />
<input id="btnSubmit" type="button" value="Show electricity consumption" @*onclick="location.href='@Url.Action("FilterByDate", "FilterByDate")'"*@ />
<br />
@section scripts{
<script src="~/Scripts/jquery-ui-1.12.1.min.js"></script>
<script>
$(function algus() {
$(".datepicker").datepicker({
dateFormat: "dd.mm.yy",
changeMonth: true,
changeYear: true,
showWeek: true,
yearRange: "2010:2018",
minDate: new Date(2010, 0, 1),
maxDate: new Date(2018, 0, 1),
showOn: "both",
buttonText: "Select"
});
});
$(function () {
$("#startdate").datepicker({ dateformat: "dd.mm.yy" });
$("#enddate").datepicker({ dateformat: "dd.mm.yy" });
$("#btnSubmit").click(function (e) {
e.preventDefault();
var startdate = $("#startdate").datepicker('getDate');
console.log(startdate);
var enddate = $("#enddate").datepicker('getDate');
console.log(enddate);
var datefilter = { StartDate: startdate, EndDate: enddate };
console.log(datefilter);
$.ajax({
url: "@Url.Action("FilterByDate", "DatePicker")",
type: "get",
data: datefilter
})
.done(function(datepicker) {
$("#res").html(tmpConsumptions);
});
});
});
</script>
}