To make a weekdays calender in MVC, firstly, we need to enter a few fields such as dates ,starting time, end time and duration between slots.

form

Let's start doing coding part for this design. I am making this form in mvc with entity Framework.

Here I am showing Code of Create.cshtml Page,

  1. @model RecruitingOutboundScheduler.Domain.recr_recruiterShift
  2. @{
  3. ViewBag.Title = "Create";
  4. }
  5. <h2>Create</h2>
  6. @using (Html.BeginForm()) {
  7. @Html.AntiForgeryToken()
  8. @Html.ValidationSummary(true)
  9. <fieldset>
  10. <legend>Recruiter Schedular</legend>
  11. <table align="center" >
  12. <tr>
  13. <td>
  14. <div class="form-group">
  15. @Html.Label("Site Name: ", htmlAttributes: new { @class = "control-label col-md-2" })
  16. </div>
  17. </td>
  18. <td>
  19. <div class="col-md-10">
  20. @Html.DropDownList("SiteList", null, htmlAttributes: new { @class = "w250p h30p bdrno" })
  21. </div>
  22. </td>
  23. </tr>
  24. <tr>
  25. <td>
  26. <div class="form-group">
  27. @Html.LabelFor(model => model.recruiterID, "Recruiter Name: ", htmlAttributes: new { @class = "w250p h30p bdrno" })
  28. </div>
  29. </td>
  30. <td>
  31. <div class="col-md-10">
  32. @Html.DropDownList("recruiterID", (IEnumerable<SelectListItem>)ViewBag.recruiterID, "--Select Recruiter--", new { @class = "w250p h30p bdrno" })
  33. @Html.ValidationMessageFor(model => model.recruiterID)
  34. </div>
  35. </td>
  36. </tr>
  37. <tr>
  38. <td>
  39. <div class="editor-label">
  40. @Html.LabelFor(model => model.startDate,"Shift Start Time")
  41. </div>
  42. </td>
  43. <td>
  44. @Html.DropDownList("startHoursList", (IEnumerable<SelectListItem>)ViewBag.startHoursList, "HH", new { @class = "w50p h30p bdrno" })
  45. @Html.DropDownList("startMinutesList", (IEnumerable<SelectListItem>)ViewBag.startMinutesList, "MM", new { @class = "w50p h30p bdrno" })
  46. </td>
  47. </tr>
  48. <tr>
  49. <td>
  50. <div class="editor-label">
  51. @Html.LabelFor(model => model.endDate,"Shift End Time")
  52. </div>
  53. </td>
  54. <td>
  55. @Html.DropDownList("endHoursList", (IEnumerable<SelectListItem>)ViewBag.endHoursList, "HH", new { @class = "w50p h30p bdrno" })
  56. @Html.DropDownList("endMinutesList", (IEnumerable<SelectListItem>)ViewBag.endMinutesList, "MM", new { @class = "w50p h30p bdrno" })
  57. </td>
  58. </tr>
  59. <tr>
  60. <td>@Html.LabelFor(model => model.recurrencePattern,"Pattern")</td>
  61. <td>
  62. @Html.DropDownList("recurrencePattern", (IEnumerable<SelectListItem>)ViewBag.Pattern, "--Select Pattern--", new { @class = "w250p h30p bdrno" })
  63. @Html.ValidationMessageFor(model => model.recurrencePattern)
  64. </td>
  65. </tr>
  66. <tr>
  67. <td>
  68. <div class="editor-label">
  69. @Html.Label("Selected Dates")
  70. </div>
  71. </td>
  72. <td>
  73. <div id="celenderForMultiple" class="celender">
  74. <div data-role="calendar" data-week-start="1" data-multi-select="true" id="c1"></div>
  75. <br />
  76. <div class="align-center">
  77. <input name="getDates" type="button" value="Create Schedule" onclick="get_dates()"/>
  78. </div>
  79. </div>
  80. </td>
  81. </tr>
  82. <tr>
  83. <td colspan="2">
  84. <div>
  85. <div id="wrapper"></div>
  86. </div>
  87. </td>
  88. </tr>
  89. </table>
  90. <p>
  91. @*<input type="hidden" id="setdateTime" value="" />*@
  92. @Html.Hidden("setStartDateTime")
  93. @Html.Hidden("setEndDateTime")
  94. @Html.Hidden("shiftStartTime")
  95. @Html.Hidden("shiftEndTime")
  96. <input type="button" value="Clear Schedule" onclick="cleardateTimeValue();" />
  97. <input type="submit" value="Save Schedule" onclick="setdateTimeValue();" />
  98. </p>
  99. </fieldset>
Here I am hiding my model name for some security purposes.

Output Layout When you Run Page

(let's say my model name is home and action name is Shift)

Output

In this I am taking dropdown for time (HH:MM) and selecting Slots as 30 minutes, 60 minutes etc.Then for date I am using Datepicker of MetroUi. Create Schedule is a Button on which I am calling javascript function to make Weekly table based on time.

Let's say starting time is 9:00 and End time is 3:00. And I need slots of 1 hour to make my schedule for selected dates.
Select Value and see output of Generated Weekly schedule.Total slots I want i.e, 18
From 9-10,10-11,……………………………………….1-2.

And I select day on which I want to show my calendar i.e, 27-may,28-may……..…10-may.

select day

Here based on selected Value weekly schedule is drawn by very simple Javascript logic.

On create Schedule Button I call simple javascript function i.e, createTable()

Code
  1. function createTable() {
  2. var num_rows = row;
  3. var num_cols = time.length;// result.length
  4. var theader = '<table id="tableID" style="border:1px solid black;">\n';
  5. var thead = '';
  6. var tbody = '';
  7. for (var h = 0; h < 1; h++) {
  8. thead += '<tr style="border:2px solid black;">';
  9. for (var j = 0; j < num_cols; j++) {
  10. if (j == 0) {
  11. thead += '<td class="head" style="border:1px solid black;">';
  12. thead += "";
  13. thead += '</td>'
  14. }
  15. else {
  16. thead += '<td class="head" style="border:1px solid black;">';
  17. thead += time[j - 1];
  18. thead += '</td>'
  19. }
  20. }
  21. thead += '</tr>\n';
  22. }
  23. for (var i = 0; i < num_rows; i++) {
  24. tbody += '<tr style="border:2px solid black;">';
  25. for (var j = 0; j < num_cols; j++) {
  26. if (j == 0) {
  27. tbody += '<td class="head" style="border:1px solid black;">';
  28. tbody += date[i];
  29. tbody += '</td>'
  30. }
  31. else {
  32. tbody += '<td class="rid" style="border:1px solid black;color:white;"> ';
  33. tbody += i +","+ j;
  34. tbody += '</td>'
  35. }
  36. }
  37. tbody += '</tr>\n';
  38. }
  39. var tfooter = '</table>';
  40. document.getElementById('wrapper').innerHTML = theader + thead + tbody + tfooter;
  41. }
Output

Output

Next work is to select slots which I want to, for this on $(document).on('click', '.rid', function () simple apply a code for mouse click.

Code
  1. $(document).on('click', '.rid', function () {
  2. var a = ($(this).html());
  3. var res = a.split(',');
  4. $(this).val("Booked");
  5. $(this).css('backgroundColor', '#00FF00');
  6. $(this).css('color', '#00FF00');
  7. var sDt = date[parseInt(res[0])] + " " + time[parseInt(res[1]) - 1];
  8. var eDt = date[parseInt(res[0])] + " " + time[parseInt(res[1])];
  9. selectedStartDateTime.push(sDt);
  10. selectedEndDateTime.push(eDt);
  11. });
Output

Output

Clear Schedule button is use to Deselect Reserve Slot.