So I am using the JQuery.datePicker example…
http://www.kelvinluck.com/assets/jquery/datePicker/v2/demo/datePickerStartEnd.html
I had the Jquery calendar working fine, but it olnly allowed me to enter 1 date at a time and save it to the DB.
I want the user to select a start and a multiple date, run a loop through the dates and each day to the DB.
Slightly lost at the minute and any help would be appreciated….
Here is part of my view for my ActionResult create()//which allows the user to add a new day…
<form action ="ListHolidays" id="listHolidays" method="post">
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Holiday</legend>
<div>
@Html.LabelFor(model => model.PersonId, "Person")
</div>
<div>
@Html.DropDownListFor(model => model.PersonId,
new SelectList(ViewBag.Id, "Value", "Text"),
"---Select---"
)
@Html.ValidationMessageFor(model => model.PersonId)
</div>
<div>
@Html.LabelFor(model => model.HolidayDate)
</div>
<div>
@Html.TextBoxFor(model => model.HolidayDate)
@Html.TextBoxFor(model => model.endDate)
<script>
$("#HolidayDate").addClass('date-pick');
$("#endDate").addClass('date-pick');
//$('.date-pick').datePicker//({dateFormat: 'dd-mm-yy'}).val();
$(function ()
{
$('.date-pick').datePicker()
$('#HolidayDate').bind('dpClosed',
function (e, selectedDates)
{
var d = selectedDates[0];
if (d)
{
d = new Date(d);
$('#endDate').dpSetStartDate(d.addDays(1).asString());
}
}
);
$('#endDate').bind('dpClosed',
function (e, selectedDates)
{
var d = selectedDates[0];
if (d)
{
d = new Date(d);
$('#HolidayDate').dpSetEndDate(d.addDays(-1).asString());
}
}
);
});
</script>
@Html.ValidationMessageFor(model => model.HolidayDate)
</div>
<p>
<input type="submit" value="Create"/>
</p>
and my post for listHolidays:
[HttpPost]
public ActionResult listHolidays(Holiday holiday, int? PersonId, string HolidayDate, string endDate)
{
IList<DateTime> dates = new List<DateTime>();
dates.Add(DateTime.Parse(HolidayDate));
dates.Add(DateTime.Parse(endDate));
List<DateTime> orderedDates = dates.OrderBy(d => d).ToList();
for (int i = 0; i < orderedDates.Count; i++ )
//if (ModelState.IsValid)
{
db.Holidays.AddObject(holiday);
db.SaveChanges();
return View();
}
return RedirectToAction("Index");
// return View();
}
The problem being in my list Holidays, im not sure how to run through the loop from start to end date, and add a new record to the DB for each one.
Please advise
I’m going to give you an example of iterating from a start date to an end date and then you can plug that into your solution:
Now, one other thing to note is that you could in fact receive the
startDateandendDateasDateTimebecause ASP.NET will do the conversion for you. If one of them is nullable then just set its type toDateTime?.