因此,用户从两个日历中选择假期(开始和结束日期),然后从开始到结束的日期存储在数据库中,每个日期的单独记录中。
(感谢上一个问题的帮助,一切正常)
现在,我想要某种错误消息,如果用户选择的日期已经存在于数据库中,则不允许单击该按钮。
我不确定这是从视图还是控制器完成的?
视图:
<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");
}
如果假期日期 = db.exisitng 日期?
不知道该怎么做。
请指教。
根据您需要做的事情,您有几个选择:
- 将现有日期预加载到某种类型的 JS 数组/集合中,然后在值更改时对该数组进行验证。
-
将 ajax 查询附加到日期选取器的已更改事件。 当用户更改日期时,请执行查询并验证该日期。
$('.date-pick').bind('dateSelected',function (e, selectedDate, $td){ $.ajax({ url:'somepath/action', data: {selectedDateVal: selectedDate}, success:function(result){ if(result == true){ //value exists }else{ //value is good } } });});
-
查看自定义数据注释。 希望我能在这方面提供更多的意见,但不幸的是,我在这方面没有很多经验。