albert albert

albert albert

  • NA
  • 524
  • 0

Saving data from dropdownlist

Jun 3 2012 5:28 AM
Hi everybody,

I have an extension method:

[code]
public static IEnumerable<SelectListItem> GetWeekNumbers(this HtmlHelper helper /*Dictionary<int, DateTime> dayOfWeek*/  /*,DateTime allWeeks*/ /*int selectID*/)
        {
            var janFirst = new DateTime(DateTime.Today.Year, 1, 1);
            //beware different cultures.
            var firstWeek = janFirst.AddDays(1 - (int)(janFirst.DayOfWeek));
            int j = -1;
            foreach (var weekNumber in Enumerable.Range(0, 52).Select(i => new
            {
                weekStart = firstWeek.AddDays(i * 7),
                //Tuesday = firstWeek.AddDays(i * 3)
            }).TakeWhile(x => x.weekStart.Year <= janFirst.Year).Select(x => new
            {
                x.weekStart,
                weekFinish = x.weekStart.AddDays(6)
            }).SkipWhile(x => x.weekFinish < janFirst.AddDays(1)))
            {
                j++;
               yield return new SelectListItem
                {
                  
                    Value = string.Join(",",new string[]
                    {
                       
                      
                       weekNumber.weekStart.ToString("dd-MM-yyyy")
                      // weekNumber.weekStart.AddDays(1),
                      // weekNumber.weekStart.AddDays(2),
                     //  weekNumber.weekStart.AddDays(3),
                      // weekNumber.weekStart.AddDays(4),
                      // weekNumber.weekStart.AddDays(5),
                      // weekNumber.weekStart.AddDays(6),

                    }),
                   
                    Text = (j + 1).ToString(CultureInfo.InvariantCulture)
                };
            }
        }//end method
[/code]

this is a dropdownlist for week numbers.

and an JQuery method;

[code]
function Selectedchange() {
    //$("#txtBox").append($('<option selected="true" value ='+ "#weekId" + '>').val().toString().split(",") [0]);
    $("#txtBox").val($("#weekId").val().toString().split(",")[0]);
            $("#txtBox1").val($("#weekId").val().toString().split(",")[1]);
            $("#lblBox2").val($("#weekId").val().toString().split(",")[2]);
            $("#lblBox3").val($("#weekId").val().toString().split(",")[3]);
            $("#lblBox4").val($("#weekId").val().toString().split(",")[4]);
            $("#lblBox5").val($("#weekId").val().toString().split(",")[5]);
            $("#lblBox6").val($("#weekId").val().toString().split(",")[6]);
    };
[/code]

for selecting the correct date associated with the weeknumber.

and the view:

[code]

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Hour</legend>
        <table style="width: 100%;">
            <tr>
                <td>
                    <div class="editor-label">
                        @Html.LabelFor(model => model.WeekID, "Week")
                    </div>
                    <div class="editor-field">
                        @Html.DropDownList("WeekID", String.Empty)
                        @Html.ValidationMessageFor(model => model.WeekID)
                    </div>
                </td>

                <td>               
                    <div class="editor-label">
                        @Html.LabelFor(model => model.Week.YearNumber, "jaar")
                    </div>
                    <div class="editor-field">
                        @Html.DropDownListFor(model => model.Week.YearNumber,Html.GetWeekOfYear())
                       
                        @Html.ValidationMessageFor(model => model.Week.YearNumber)
                    </div>
                </td>

                <td>
               
                    <div class="editor-label">
                        @Html.LabelFor(model => model.WeekNumber, "week")
                    </div>
                    <div class="editor-field">
                        @Html.DropDownList("weekId", (IEnumerable<SelectListItem>)Html.GetWeekNumbers(), new { onchange = "Selectedchange();" })
                        @Html.ValidationMessage("week")
                    </div>
                </td>



                <td>
                    <div class="editor-label">
                        @Html.LabelFor(model=> model.Monday)
                    </div>
                    <div class="editor-field">
                        @Html.TextBox("txtBox", Model.Monday.ToShortDateString())
                        @Html.ValidationMessageFor(model => model.Monday)
                    </div>
                </td>
               
                  <td>
                    <div class="editor-label">
                        @Html.LabelFor(model => model.DateNow)
                    </div>
                    <div class="editor-field">
                        @Html.TextBox("DateNow",Model.DateNow.ToShortDateString())
                        @Html.ValidationMessageFor(model => model.DateNow)
                    </div>
                </td>

               

            </tr>
            <tr>
                <td>
             
                </td>
                <td>
               
                </td>
                <td>
                  
                  
                </td>

                <td>
                   
                     <div class="editor-field">
                        @Html.EditorFor(model => model.MondayHours)
                        @Html.ValidationMessageFor(model => model.MondayHours)
                    </div>
                </td>
              
              
               

              

               



            </tr>
            <tr>
           
            </tr>
        </table>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}
<div>
    @Html.ActionLink("Back to List", "Index")
</div>

[/code]

So I have a dropdownlist with week numbers. And if u select a weeknumber then u will see in the textbox for Monday the correct data of monday, for example: week 1 of year 2012 will return in the textbox of Monday: 02-01-2012. But if I try to commit it to the database. nothing will happen. How to commit the data to the database?

This are the functions of the HourController:

[code]
public ActionResult Create()
        {
            ViewBag.WeekID = new SelectList(db.Weeks, "WeekID", "WeekID");
            return View(new Hour());
        }

        //
        // POST: /Hour/Create

        [HttpPost]
        public ActionResult Create(Hour hour)
        {

            try
            {
                if (ModelState.IsValid)
                {
                    db.Hours.Add(hour);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
            }
            catch (DataException)
            {
                    ModelState.AddModelError("",hour.Monday.ToString());
              
            }
           

            ViewBag.WeekID = new SelectList(db.Weeks, "txtBox", "txtBox", hour.WeekID);
            return View(hour);
        }
[/code]

THX