简体   繁体   中英

ASP.NET MVC - Post action in partial view returns a null ViewModel

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM