0

I have a problem sending a list of objects to my controller. For some reason I keep getting a list with count=0.

This is the model I send :

public class DIYViewModel
{

    public List<Item> Items { get; set; }
    public List<CheckModel> Checklist { get; set; }
    public int Page { get; set; }
    public int TotalPages { get; set; }
    public DIYViewModel(int page, List<Item> items,List<CheckModel> checklist)
    {
        int index = (page - 1) * 10;
        this.Items = items.Skip(index).Take(10).ToList();
        this.Page = page;
        this.TotalPages = ((items.Count - 1) / 10) + 1;
        this.Checklist = checklist;
    }
}

This is the CheckModel :

public class CheckModel
{
    public int Id
    {
        get;
        set;
    }
    public string Name
    {
        get;
        set;
    }
    public bool Checked
    {
        get;
        set;
    }
}

This is the View that gets a DIYViewModel:

@model Homeserve.Web.Models.DIYViewModel
@using Sitecore.Data.Items
@using Homeserve.Web.Models.Helpers
@using (Html.BeginForm("Testing", "DIY", new {  app=Model.Checklist }))
{
foreach (CheckModel item in Model.Checklist)
{
    @Html.HiddenFor(it => item.Id)
    @Html.DisplayFor(it => item.Name)
    @Html.CheckBoxFor(it => item.Checked)
}
<input id="Submit1" type="submit" value="submit" />
}
@{
foreach (Item newsItem in Model.Items)
{
    <p> @Html.Sitecore().Field("Article Title", newsItem)</p>
    <p> @Html.Sitecore().Field("Article Date", newsItem)</p>
    <p> @Html.Sitecore().Field("Contents", newsItem)</p>
    <p> @Html.Sitecore().Field("Article Image", newsItem)</p>
    <p>social media stuff here</p>
}
}
@Html.DIYsPagination(Model.Page, Model.TotalPages)

My controller:

[HttpPost]
public ActionResult Testing(List<CheckModel> app)
{
    return View();
}

List app is always a list with 0 items even though in the view all is working perfectly.

Ilya Chumakov
  • 23,161
  • 9
  • 86
  • 114
Andrei
  • 17
  • 7

1 Answers1

1

You have to use a for loop instead of foreach, and use index to save your data.

@for (int i = 0; i < Model.Checklist.Count ;i++)
{
    @Html.DisplayFor(it => it.Checklist[i].Name)
    @Html.HiddenFor(it =>  it.Checklist[i].Id)
    @Html.CheckBoxFor(it =>  it.Checklist[i].Checked)
}
AdrienTorris
  • 9,111
  • 9
  • 34
  • 52
  • In addition, your model in the view is `@model DIYViewModel` so your POST method needs to be `public ActionResult Testing(DIYViewModel app)` –  Feb 04 '16 at 00:16
  • @StephenMuecke i tried like that too but it kept being null, ill try the for method with DIYViewModel as a model – Andrei Feb 04 '16 at 07:15
  • @user1705561, Yes, it must be a `for` loop (or use a custom `EditorTemplate` for `CheckModel` and in the view use `@Html.EditorFor(m => m.Checklist)`) A `foreach` loop generates duplicate `name` attributes that have no relationship to your model and duplicate `id` attributes which is invalid html (you should inspect the actual html before and after doing this to understand) –  Feb 04 '16 at 07:19
  • @StephenMuecke I tried with for and with the model that i sent but now instead of passing count=0 it passes null. Another question i have, the code looks like this now:@using (Html.BeginForm("Testing", "DIY", new { app= Model })) { for (int i = 0; i < Model.Checklist.Count; i++) { @Html.DisplayFor(it => it.Checklist[i].Name) @Html.HiddenFor(it => it.Checklist[i].Id) @Html.CheckBoxFor(it => it.Checklist[i].Checked) } } the model i sent has more fields than just those set to display, do i need to – Andrei Feb 04 '16 at 07:53
  • add those too?. Also this is the html it generates: Cooling for the first item. – Andrei Feb 04 '16 at 07:57
  • @user1705561 Looks like what (no one can read your code in comments especially when you do not even format it)? If this is not working, then you have made an error. Post a new question showing the new code you have tried (or append it to this question) so it can be corrected. (and you need to remove `new { app= Model }` from you form –  Feb 04 '16 at 07:59
  • yea i did that but still didn't work, also sorry bout the code in the comments, didnt know how to format it – Andrei Feb 04 '16 at 13:18
  • ok so the problem was that my model didn't have an implicit constructor. thanks for the help – Andrei Feb 04 '16 at 13:45