I am currently working on an asp.net mvc project which allows users to select only available dates from a calendar in order to book a specific service. I currently have a Datepicker.cs model which holds a DateTime Date field and an int Id field and I have a DbContext class which I use to persist and retrieve this data
namespace Calendar.Models
{
public class CalendarDb : DbContext
{
public DbSet<Datepicker> Dates { get; set; }
}
}
I am using jQuery UI Datepicker on my Create.cshtml page to show the available dates to users. Currently the calendar shows 2 hardcoded available dates.
Now my question is, is it possible in the asp.net mvc framework to populate this hardcoded array (or even a list etc.) with all of the dates in the Date column in the Datepickers Db? i.e. I need the calendar to show all the available dates in the database
Any help would be greatly appreciated!
My Datepicker model:
namespace Calendar.Models
{
public class Datepicker
{
public DateTime DtmDate { get; set; }
public int Id { get; set; }
public int UserId { get; set; }
}
}
Controller for the view:
[HttpGet]
public ActionResult Create()
{
var model = _db.Dates.ToList();
return View(model);
}
My Create.cshtml view as it stands:
@model Calendar.Models.Datepicker
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Datepicker</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.DtmDate, htmlAttributes: new { @class
= "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DtmDate, new { htmlAttributes =
new { @class = "form-control1" } })
@Html.ValidationMessageFor(model => model.DtmDate, "", new {
@class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserId, htmlAttributes: new { @class =
"control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserId, new { htmlAttributes =
new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.UserId, "", new {
@class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
<link href="~/Content/themes/base/jquery-ui.min.css" rel="stylesheet" />
@section scripts {
<script src="~/Scripts/jquery-ui-1.12.1.min.js"></script>
<script>
jQuery(function(){
var enableDays = ["11-07-2019", "12-07-2019"];
function enableAllTheseDays(date) {
var sdate = $.datepicker.formatDate('dd-mm-yy', date)
console.log(sdate)
if ($.inArray(sdate, enableDays) != -1) {
return [true];
}
return [false];
}
$(".form-control1").datepicker({
dateFormat: "dd/MM/yy",
beforeShowDay: enableAllTheseDays
});
});
</script>
@Scripts.Render("~/bundles/jqueryval")
}
<style>
.my-class a {
background-color: #07ea69 !important;
background-image :none !important;
color: #ffffff !important;
}
</style>