0

I have a View with a list of Brands in a checklist, where you choose the Brand you are interested in and in the Controller it formats that information into an Email. The Brand list is created dynamically through the database and has about 15 items in the list. When it gets passed back to the Controller, however, only the first 5 items are listed. I don't think I am missing anything, but I can't find a reason why all this data would be disappearing.

Here is my code (with only the important bits)

View

@using (Html.BeginForm("RegisterByEmail", "Home", FormMethod.Post, new { @class = "forms", role = "form" }))
{
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    if (TempData["emailStatus"] == null)
    {
        <div class="row checkbox noMargin" id="chklstBrands">
            <div class="col-sm-10 col-sm-offset-2">
            @{
                for (int i = 0; i < Model.BrandList.Count(); i++)
                {
                    var name = Model.BrandList[i];
                    @Html.HiddenFor(model => model.BrandList[i].BrandName)
                    @Html.CheckBoxFor(model => model.BrandList[i].Checked)
                    @Html.LabelFor(model => model.BrandList[i].BrandName, name.BrandName)
                    <br />
                }
             }
             </div>
        </div>

        <div class="col-sm-12 text-center">
                <input type="submit" class="btn btn-default" value="Submit" />
       </div>
    }
}

When you hit submit, goes to the Controller. Here's a snippet of the code:

Controller

[HttpPost]
public ActionResult RegisterByEmail(RegisterByEmailModel vm)
{
    if (ModelState.IsValid)
    {
        Register register = new Register
        { //other fields omitted
            Interest = BuildInterest(vm.NewsEvents, vm.RequestType, vm.BrandList)
        };

When you examine the data while debugging, vm.BrandList has a Count of 5. When opening the Raw View, it shows the following values:

Capacity = 8

Count = 5

I am not sure what I am missing and I tried a thorough search of the site before posting this question, so I apologize if it is a duplicate.

EDIT Adding the Model below:

public class RegisterByEmailModel
{
    public string EmailTo { get; set; }

    public string Subject { get; set; }

    public string TemplateFileName { get; set; }

    public string LogoURL { get; set; }

    [Display(Name = "Company Name")]
    [Required(ErrorMessage = "Company Name is Required")]
    public string CompanyName { get; set; }

    [Required(ErrorMessage = "Contact Name is Required")]
    [Display(Name = "Contact Name")]
    public string ContactName { get; set; }

    [Display(Name = "Email")]        
    [Required(ErrorMessage = "The email address is required")]
    [EmailAddress]
    public string Email { get; set; }

    [Display(Name = "Account Number")]
    public string AccountNumber { get; set; }

    [Display(Name = "Telephone")]
    [Required(ErrorMessage = "Telephone is Required")]
    public string Telephone { get; set; }

    [Display(Name = "Address Line 1")]
    [Required(ErrorMessage = "Address is Required")]
    public string AddressLine1 { get; set; }

    [Display(Name = "Address Line 2")]
    public string AddressLine2 { get; set; }

    [Display(Name = "Zip Code")]
    [Required(ErrorMessage = "Zip Code is Required")]
    public string ZipCode { get; set; }

    [Display(Name = "Interest")]
    public string Interest { get; set; }

    [Display(Name = "I would like to be updated on news and events")]
    public bool NewsEvents { get; set; }

    [DataType(DataType.MultilineText)]
    public string Comments { get; set; }

    public List<BrandCheckBoxViewModel> BrandList { get; set; }

    public AccountRequestTypes RequestType { get; set; }
    public string FacebookURL { get; set; }
}
public class BrandCheckBoxViewModel
{
    public bool Checked { get; set; }
    public string BrandName { get; set; }
}
public enum AccountRequestTypes
{
    [Display(Name = "I would like an account")]
    NewAccount,
    [Display(Name = "I have an account and would like to request additional brands:")]
    BrandRequest
}

Lastly, as requested, the content of this.Request.Form: (I am not sure how to format this better, sorry - and this is technically this.Request.Form._allEntriesArray, but this is where the data was visible)

  • [9] {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} object {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} Key "BrandList[0].BrandName" string
  • Value Count = 1 object {System.Collections.ArrayList}
  • [10] {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} object {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} Key "BrandList[0].Checked" string
  • Value Count = 1 object {System.Collections.ArrayList} [0] "false" object {string}
  • Raw View
  • [11] {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} object {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} Key "BrandList[1].BrandName" string
  • Value Count = 1 object {System.Collections.ArrayList}
  • [12] {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} object {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} Key "BrandList[1].Checked" string
  • Value Count = 2 object {System.Collections.ArrayList} [0] "true" object {string} [1] "false" object {string}
  • Raw View
  • [13] {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} object {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} Key "BrandList[2].BrandName" string
  • Value Count = 1 object {System.Collections.ArrayList}
  • [14] {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} object {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} Key "BrandList[2].Checked" string
  • Value Count = 1 object {System.Collections.ArrayList} [0] "false" object {string}
  • Raw View
  • [15] {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} object {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} Key "BrandList[3].BrandName" string
  • Value Count = 1 object {System.Collections.ArrayList}
  • [16] {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} object {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} Key "BrandList[3].Checked" string
  • Value Count = 1 object {System.Collections.ArrayList} [0] "false" object {string}
  • Raw View
  • [17] {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} object {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} Key "BrandList[4].BrandName" string
  • Value Count = 1 object {System.Collections.ArrayList}
  • [18] {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} object {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} Key "BrandList[4].Checked" string
  • Value Count = 1 object {System.Collections.ArrayList} [0] "false" object {string}
  • Raw View
  • [19] {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} object {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} Key "BrandList[7].BrandName" string
  • Value Count = 1 object {System.Collections.ArrayList}
  • [20] {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} object {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} Key "BrandList[7].Checked" string
  • Value Count = 2 object {System.Collections.ArrayList} [0] "true" object {string} [1] "false" object {string}
  • Raw View
  • [21] {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} object {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} Key "BrandList[8].BrandName" string
  • Value Count = 1 object {System.Collections.ArrayList}
  • [22] {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} object {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} Key "BrandList[8].Checked" string
  • Value Count = 1 object {System.Collections.ArrayList} [0] "false" object {string}
  • Raw View
  • [23] {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} object {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} Key "BrandList[9].BrandName" string
  • Value Count = 1 object {System.Collections.ArrayList}
  • [24] {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} object {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} Key "BrandList[9].Checked" string
  • Value Count = 1 object {System.Collections.ArrayList} [0] "false" object {string}
  • Raw View
  • [25] {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} object {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} Key "BrandList[10].BrandName" string
  • Value Count = 1 object {System.Collections.ArrayList}
  • [26] {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} object {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} Key "BrandList[10].Checked" string
  • Value Count = 1 object {System.Collections.ArrayList} [0] "false" object {string}
  • Raw View
  • [27] {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} object {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} Key "BrandList[11].BrandName" string
  • Value Count = 1 object {System.Collections.ArrayList}
  • [28] {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} object {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} Key "BrandList[11].Checked" string
  • Value Count = 2 object {System.Collections.ArrayList} [0] "true" object {string} [1] "false" object {string}
  • Raw View
  • [29] {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} object {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} Key "BrandList[12].BrandName" string
  • Value Count = 1 object {System.Collections.ArrayList}
  • [30] {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} object {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} Key "BrandList[12].Checked" string
  • Value Count = 1 object {System.Collections.ArrayList} [0] "false" object {string}
  • Raw View
  • [31] {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} object {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} Key "BrandList[13].BrandName" string
  • Value Count = 1 object {System.Collections.ArrayList}
  • [32] {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} object {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} Key "BrandList[13].Checked" string
  • Value Count = 1 object {System.Collections.ArrayList} [0] "false" object {string}
  • Raw View
  • [33] {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} object {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} Key "BrandList[14].BrandName" string
  • Value Count = 1 object {System.Collections.ArrayList}
  • [34] {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} object {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} Key "BrandList[14].Checked" string
  • Value Count = 2 object {System.Collections.ArrayList} [0] "true" object {string} [1] "false" object {string}
  • Raw View
  • [35] {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} object {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} Key "BrandList[15].BrandName" string
  • Value Count = 1 object {System.Collections.ArrayList}
  • [36] {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} object {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} Key "BrandList[15].Checked" string
  • Value Count = 1 object {System.Collections.ArrayList} [0] "false" object {string}
  • Raw View
  • [37] {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} object {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} Key "BrandList[16].BrandName" string
  • Value Count = 1 object {System.Collections.ArrayList}
  • [38] {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} object {System.Collections.Specialized.NameObjectCollectionBase.NameObjectEntry} Key "BrandList[16].Checked" string
  • Value Count = 1 object {System.Collections.ArrayList}
14
  • Do you have a raw request data? Are all 15 entries there? Commented Feb 26, 2018 at 16:41
  • @Andrei Yes, when I debug the View page during setup, Model.BrandList has all 15 entries. Commented Feb 26, 2018 at 16:59
  • That's not what I am asking though. When you submit your request, what parameters does it contain? Is it only 5 entries, or all 15? Commented Feb 26, 2018 at 17:00
  • @Andrei Apologies, I misunderstood. I am not sure I remember how to check that. Commented Feb 26, 2018 at 17:04
  • @Andrei Checked again, when I hit submit and jump to the Controller, this.Request.Form seems to have all the expected keys. Commented Feb 26, 2018 at 17:12

1 Answer 1

1

First and foremost, the problem came from an omitted if statement that I did not think was relevant to the issue at hand. So, shame on me.

In the View, the section of code that created the list with checkboxes actually looked like this:

@{
     for (int i = 0; i < Model.BrandList.Count(); i++)
     {
          string columbia = "Columbia";
          string choiceRewards = "Choice Rewards Preview";
          if (!Model.BrandList[i].BrandName.Contains(columbia) & !Model.BrandList[i].BrandName.Contains(choiceRewards))
          {
               var name = Model.BrandList[i];
               @Html.HiddenFor(model => model.BrandList[i].BrandName)
               @Html.CheckBoxFor(model => model.BrandList[i].Checked)
               @Html.LabelFor(model => model.BrandList[i].BrandName, name.BrandName)
               <br />
           }
     }

}

It turns out that preventing these two choices from becoming checkboxes messes with the process, and stops after BrandList[4] because it cannot find BrandList[5].

Removing the if statement and debugging showed all the expected results in the list, so now all I need to do is find a better way to exclude those two options from the list (which should be easy), and tell my coworker that whoever did it to begin with didn't do a good job of it.

Sign up to request clarification or add additional context in comments.

2 Comments

Only pass the data you require to the view (i.e. filter the collection in the controller method)
@StephenMuecke yes, once I knew where the issue was I knew exactly where to limit it. Thank you for your help!

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.