2016-05-13 69 views
1

我目前建立ASP.NET C#MVC應用程序。我打算使用HTML格式的引導日期選擇器多點日期來讓用戶選擇幾個日期(假期)。問題是datepicker在一個輸入字段中輸入多個日期,並用逗號分隔(例如:2016-10-10,2016-11-10,2016-13-10)。我想知道如何寫這些日期每一條記錄在數據庫中,使用這種形式的其他輸入(姓名,地址等)反覆輸入這樣一個沿:輸入引導多日期值到幾個SQL記錄

enter image description here

這裏是我的模型:

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() 
       }); 

謝謝你們的幫助,

+0

你必須用逗號分開leave.B.LvDate,然後做一個foreach循環 –

回答

1

如果你只想在LeaveApps表中的記錄,但有mulptiple日期,我建議你LeavAppDates將由EMPID鏈接創建一個單獨的表。

然後,將員工數據插入離開應用程序,在日期列表中進行逗號分隔,並循環查看日期,並在新的LeavsAppDates中插入具有該EmpId的日期。

否則

我想這個字符串你從日期選擇器獲取轉換到一個逗號分隔的列表

var datePickerDateList =leave.B.LvDate.Split(',').Select(i => DateTime.Parse(i)).ToList(); 

下一步將遍歷列表就像費利佩Deguch提到

var rowsAffected = 0; 

foreach(var date in datePickerDateList) 
{ 
     var a = leave.B.LvAppId; 
     var b = leave.B.EmpId; 
     var c = date.ToString("yyyy-MM-dd"); 
     var e = leave.B.LvPeriod; 
     var f = leave.B.LvDesc; 
     //Value.ToString("yyyy-MM-dd HH:mm:ss") 
     db.Database.ExecuteSqlCommand("insert into LeaveApps (LvAppId, EmpId, LvDate, LvPeriod,LvDesc)"+ 
           " values ({0}, {1},{2},{3}, {4})", 
           "LV0022",b,c, e,f); 
     rowsAffected++; 
} 

ViewBag.RowsAffected = rowsAffected; 
+0

非常感謝你,但是我再次遇到問題,因爲事實證明我的le ave.b.LvDataata類型是日期時間,因爲我的LvDate列在sql服務器也日期時間,和datepicker保存輸入字符串我猜?你知道如何解決這個問題嗎? – Fimblutterr

+1

您需要將字符串轉換爲datetime。看看這個鏈接http://stackoverflow.com/questions/15738608/converting-dd-mm-yyyy-formatted-string-to-datetime。讓我知道如果你仍然有問題 – Sl1ver

+0

是的,它的工作原理,謝謝你 – Fimblutterr