我目前建立ASP.NET C#MVC應用程序。我打算使用HTML格式的引導日期選擇器多點日期來讓用戶選擇幾個日期(假期)。問題是datepicker在一個輸入字段中輸入多個日期,並用逗號分隔(例如:2016-10-10,2016-11-10,2016-13-10)。我想知道如何寫這些日期每一條記錄在數據庫中,使用這種形式的其他輸入(姓名,地址等)反覆輸入這樣一個沿:輸入引導多日期值到幾個SQL記錄
這裏是我的模型:
public class LeaveApp
{
[Key]
public int LvId { get; set; }
public string LvAppId { get; set; }
public int EmpId { get; set; }
public DateTime LvDate { get; set; }
public string LvPeriod { get; set; }
public string LvDesc { get; set; }
}
是發佈到數據庫
而且我的控制器:
[HttpPost]
public ActionResult Create(/*[Bind(Include = "EmpId,LvAppId,LvPeriod,LvStartDate,LvEndDate,LvDesc,LvCount")]*/ LeaveApplicationViewModels leave)
{
if (leave != null)
{
var a = leave.B.LvAppId;
var b = leave.B.EmpId;
var c = leave.B.LvDate.ToString("yyyy-MM-dd");
var e = leave.B.LvPeriod;
var f = leave.B.LvDesc;
//Value.ToString("yyyy-MM-dd HH:mm:ss")
ViewBag.RowsAffected = db.Database.ExecuteSqlCommand("insert into LeaveApps (LvAppId, EmpId, LvDate, LvPeriod,LvDesc)"+
" values ({0}, {1},{2},{3}, {4})",
"LV0022",b,c, e,f);
}
return RedirectToAction("Index");
}
而我的HTML表單使用日期選擇器:
<div class='input-group date' id="datetimepicker">
@Html.TextBoxFor(model => model.B.LvDate, new { Id = "lv_start_date", @Class = "form-control input-sm" })
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
<script type="text/javascript">
$(function() {
$('#datetimepicker').datepicker({
format: "dd/mm/yyyy",
multidate: 5,
maxViewMode: 1,
multidateSeparator: ",",
orientation: "bottom left",
keyboardNavigation: false,
clearBtn: true,
daysOfWeekDisabled: "0,6",
startDate: new Date()
});
謝謝你們的幫助,
你必須用逗號分開leave.B.LvDate,然後做一個foreach循環 –