输入Bootstrap Multidate值到几个SQL记录

时间:2016-05-13 10:00:20

标签: c# asp.net asp.net-mvc bootstrap-datepicker

我目前正在构建ASP.NET C#MVC App。我计划在html表单中使用bootstrap datepicker multidate让用户选择几个日期(度假)。问题是datepicker在一个输入字段中写入多个日期并用逗号分隔(例如:2016-10-10,2016-11-10,2016-13-10)。我想知道如何为数据库中的一条记录编写这些日期,以及该表格中的其他输入(名称,地址等),如下所示:

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

我使用datepicker的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()
                });

谢谢你的帮助,

1 个答案:

答案 0 :(得分:1)

如果你只想在LeaveApps表中有一条记录但是有多个日期,我建议你为LeavAppDates创建一个单独的表,它将由EmpId链接。

然后将员工数据插入到离开应用程序中,在日期列表上进行逗号分隔,然后循环显示日期并在新的LeavsAppDates中插入带有该EmpId的日期。

否则

我首先将你从datepicker获得的字符串转换为以逗号分隔的列表

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

下一步是像Felipe 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;