因此,用户从两个日历中选择假期(开始和结束日期),然后从开始到结束的日期存储在数据库中每个日期的单独记录中。
(感谢上一个问题的帮助,这一切都很好)
现在,我想要某种错误消息,如果用户选择的日期已经存在于数据库中,则不允许单击按钮。
我不确定这是从视图还是控制器完成?
看法:
<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>
// Date.format = 'dd/m/yyy';
$("#HolidayDate").addClass('date-pick');
$("#endDate").addClass('date-pick');
//$('.date-pick').datePicker//({dateFormat: 'dd-mm-yy'}).val();
// clickInput: true
$(function () {
//3 methods below dont allow user to select weekends
$('.date-pick').datePicker(
{
createButton: false,
renderCallback: function ($td, thisDate, month, year)
{
if (thisDate.isWeekend())
{
$td.addClass('weekend');
$td.addClass('disabled');
}
}
}
)
.bind('click',
function ()
{
$(this).dpDisplay();
this.blur();
return false;
}
)
.bind('dateSelected',
function (e, selectedDate, $td)
{
console.log('You selected ' + selectedDate);
}
);
// HolidayDate is start date
$('#HolidayDate').bind('dpClosed',
function (e, selectedDates)
{
var d = selectedDates[0];
if (d)
{
d = new Date(d);
$('#endDate').dpSetStartDate(d.addDays(1).asString());
}
}
);
//end date is end date
$('#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>
控制器:
[HttpPost]
public ActionResult listHolidays(Holiday holiday, int? PersonId, string HolidayDate, string endDate)
{
DateTime startDates = Convert.ToDateTime(HolidayDate),
endDates = Convert.ToDateTime(endDate);
while (startDates <= endDates)
{
if (startDates.DayOfWeek != DayOfWeek.Saturday && startDates.DayOfWeek != DayOfWeek.Sunday)
{
Holiday holiday1 = new Holiday();
holiday1.PersonId = PersonId.Value;
holiday1.HolidayDate = startDates;
db.Holidays.AddObject(holiday1);
db.SaveChanges();
//say start date is 10. AddDays(1) will make it 11 then return it to startDates in 'startDates' = startdates,
//but doesnt chage the value of startdates = 'startdates'
}
startDates = startDates.AddDays(1);
}
return RedirectToAction("Index");
}
如果holidayDate = db.exisitng 日期?
不确定如何去做。
请指教。