Click here to Skip to main content
       

.NET Framework

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionrun through startDate to endDate and add each record to DBmemberxnaLearner10-Dec-12 2:04 
So I am using the JQuery.datePicker example... http://www.kelvinluck.com/assets/jquery/datePicker/v2/demo/datePickerStartEnd.html
 
I had the Jquery calendar working fine, but it olnly allowed me to enter 1 date at a time and save it to the DB.
 
I want the user to select a start and a multiple date, run a loop through the dates and each day to the DB.
 
Slightly lost at the minute and any help would be appreciated....
 
Here is part of my view for my ActionResult create()//which allows the user to add a new day.
 
<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>
 
    $("#HolidayDate").addClass('date-pick');
    $("#endDate").addClass('date-pick');
        //$('.date-pick').datePicker//({dateFormat: 'dd-mm-yy'}).val();

        $(function () 
        {
            $('.date-pick').datePicker()
            $('#HolidayDate').bind('dpClosed',
                function (e, selectedDates) 
                {
                    var d = selectedDates[0];
                    if (d) 
                    {
                        d = new Date(d);
                        $('#endDate').dpSetStartDate(d.addDays(1).asString());
                    }
                }
        );
            $('#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>
 
and my post for listHolidays:
[HttpPost]
public ActionResult listHolidays(Holiday holiday, int? PersonId, string HolidayDate, string endDate)
    {
        IList<DateTime> dates = new List<DateTime>();
        dates.Add(DateTime.Parse(HolidayDate));
        dates.Add(DateTime.Parse(endDate));
        List<DateTime> orderedDates = dates.OrderBy(d => d).ToList();
 
            for (int i = 0; i < orderedDates.Count; i++ )
            //if (ModelState.IsValid)
            {
                db.Holidays.AddObject(holiday);
                db.SaveChanges();
                return View();
            }
 
            return RedirectToAction("Index");
           // return View();
        }
 
The problem being in my list Holidays, im not sure how to run through the loop from start to end date, and add a new record to the DB for each one.
 
Please advise
AnswerRe: run through startDate to endDate and add each record to DB PinmemberxnaLearner10-Dec-12 2:35 
I've updated my code to:
[HttpPost]
public ActionResult listHolidays(Holiday holiday, int? PersonId, string HolidayDate, string endDate)
    {
        DateTime startDates = Convert.ToDateTime(HolidayDate),
                 endDates = Convert.ToDateTime(endDate);
 
        while (startDates < endDates)
        {
            startDates.AddDays(1);
            db.Holidays.AddObject(holiday);
            db.SaveChanges();
        }
 
        return RedirectToAction("Index");
 
however I debugged using breakpoint, it runs through the loop once but on the second attempt it stops at db.Holidays.Addobject(holiday);
 
and I receive the error: An object with the same key already exists in the ObjecttateManager. the existing objects is in the unchanged state. An ibject can only be added to the objectStateManager again if it is i the added state.
 
if anyone can help that would be great

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   


Advertise | Privacy | Mobile
Web01 | 2.6.130617.1 | Last Updated 19 Jun 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid