2

I'm developping a website in ASP.NET MVC 4 and in one of my views, I've got 2 partial views :

<div id="timeDaysAndBeds">
    <div id="timeAndBeds">
        @Html.Action("TimeAndBedParameters")
    </div>

    <div id="daysOff">
        @Html.Action("DaysOff")
    </div>
</div>

My problem occurs in the second partial view. First, here's my GET method :

    [HttpGet]
    public ActionResult DaysOff()
    {
        context = new SchedulingDataClassesDataContext();
        List<DBDayOfWeekOff> listDaysOff = (from doff in context.DBDayOfWeekOff select doff).ToList();

        DaysOffViewModel dovm = new DaysOffViewModel();

        string[] tableDays = new string[]
        {
            "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi","Dimanche"
        };

        List<DaysViewModel> listDays = new List<DaysViewModel>();

        for (int i = 1; i <= tableDays.Length; i++)
        {
            DaysViewModel dvm = new DaysViewModel()
            {
                DayId = i,
                DayName = tableDays[i - 1],
                IsOff = false
            };

            dovm.DaysOff.Add(dvm);
        }

        foreach (DBDayOfWeekOff dayOff in listDaysOff)
        {
            dovm.DaysOff.ElementAt(dayOff.DayOfWeekNumber - 1).IsOff = dayOff.IsOff;
        }

        return PartialView(dovm);
    }

And here's the View Models I'm using :

public class DaysViewModel
{
    public int DayId { get; set; }

    public string DayName { get; set; }

    public bool IsOff { get; set; }
}

public class DaysOffViewModel
{
    public List<DaysViewModel> DaysOff { get; set; }

    public DaysOffViewModel()
    {
        DaysOff = new List<DaysViewModel>();
    }
}

So, when I'm invoking my GET method, I'm getting my partial view eand everything seems to be okay. However, when I'm trying to call my POST method, I got an empty DaysOff list (count = 0). Here's my View and my POST method :

@model AstellasSchedulerV2.Models.DaysOffViewModel

@using (Html.BeginForm("DaysOff", "Home", FormMethod.Post, new { dovm = Model }))
{
    <fieldset id="fsDaysOff">
        <legend>Jours de fermeture</legend>
        @foreach (AstellasSchedulerV2.Models.DaysViewModel dvm in Model.DaysOff)
        {
            <div class="editor-label">
                @dvm.DayName
            </div>
            <div class="editor-field">
                @Html.CheckBoxFor(d => dvm.IsOff)
            </div>
        }

        <input id="registerDayOffBtn" type="submit" name="Enregistrer" value="Enregistrer" />
    </fieldset>
}

And my POST mehtod :

    [HttpPost]
    public ActionResult DaysOff(**DaysOffViewModel dovm**=> the List "DaysOff" has no item at all)
    {
        //some code
    }

As I said, in my POST mehtod gets a DaysOffViewModel which has an empty list. What could it be?

1 Answer 1

4

Instead for each loop use for loop like this:

@for (int i=0; i<Model.DaysOff.Count; i++)
{
  <div class="editor-label">
     @Model.DaysOff[i].DayName
  </div>
  <div class="editor-field">
  @Html.CheckBoxFor(d =>  Model.DaysOff[i].IsOff)
  </div>
 }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.