1

Im currently build ASP.NET C# MVC App. I am planning to use bootstrap datepicker multidate in html form to let user select several dates (for vacation). The problem is the datepicker write multiple dates in one input field and separated by comma (example: 2016-10-10, 2016-11-10, 2016-13-10). I want to know how to write these dates each for one record in database, along with other input in that form(name,address,etc) that inputted repeatedly like this one:

enter image description here

Here is my model:

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

}

And my controller that post into database:

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

And my html form that use datepicker:

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

Thankyou for your help,

FLICKER
  • 6,439
  • 4
  • 45
  • 75
Fimblutterr
  • 89
  • 2
  • 10

1 Answers1

1

If you only want one record in LeaveApps table but have mulptiple dates I would suggest you create a seperate Table for LeavAppDates that will link by EmpId.

So then insert the employee data into leave apps, do comma seperation on the date list and and loop through the dates and insert the dates with that EmpId in the new LeavsAppDates.

Otherwise

I would first convert that string you get from the datepicker to a comma separated list

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

The next step would be to loop through the list like Felipe Deguch mentions

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;
CompiledIO
  • 160
  • 16
  • Thank you so much, but im having problem again because it turns out that my leave.b.LvDatedata type is datetime because my LvDate column in sql server also datetime, and datepicker is saving input in string i guess? do u know how to fix this? – Fimblutterr May 16 '16 at 02:24
  • 1
    You would need to convert the string to datetime. have a look at this link http://stackoverflow.com/questions/15738608/converting-dd-mm-yyyy-formatted-string-to-datetime .Let me know if you are stilling having problems – CompiledIO May 16 '16 at 06:33