Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7552701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:50:55+00:00 2026-05-30T10:50:55+00:00

I have a problem which I don’t understand and there doesn’t seem to be

  • 0

I have a problem which I don’t understand and there doesn’t seem to be an easy way to debug the problem. I’m sure it’s simple.

@model StartStop.ServiceResources.UserSettings

my MVC3 view is bound a specific model;

 public class Setting
{
    public Int64 SettingID { get; set; }
    public Int64 UserID { get; set; }
    public int PreferenceType { get; set; }
    public string PreferenceName { get; set; }
    public bool PreferenceBool { get; set; }
    public int PreferenceInt { get; set; }
    public string PreferenceString { get; set; }
    public DateTime CreatedOn { get; set; }
    public DateTime ModifiedOn { get; set; }

}

public class UserSettings
{
    public Int64 UserID { get; set; }
    public List<Setting> Settings { get; set; }
}

the view lists out the check boxes which represent the list;

  @using (Html.BeginForm("ManageAccount","Account", FormMethod.Post))
        {
            <table class="tbl" cellspacing="0">

                <tr>
                    <th>Preference</th>
                    <th>Setting</th>
                </tr>

                @if (Model != null)
                {
                    foreach (var item in Model.Settings.ToList())
                    {
                        <tr>
                            <td>@item.PreferenceName
                            </td>
                            <td>
                                @if (item.PreferenceType == 2)
                                {
                                    @Html.CheckBoxFor(modelItem => item.PreferenceBool)
                                }
                            </td>
                        </tr>

                    }
                }
            </table>

            <input type="submit" value="Save Changes" class="action medium" />

        }

All good, I load the data into the view it renders the view and picks up the correct settings. However, when I do a post at the bottom, the view model returns a null! I’m not sure why…

 [HttpPost]
    [Authorize]
    public ActionResult ManageAccount(StartStop.ServiceResources.UserSettings model)
    {
        if (ModelState.IsValid)
        {

            foreach (StartStop.ServiceResources.Setting oSetting in model.Settings)
            {
                StartStop.Helpers.UserPreferences.SaveUserSetting(oSetting);
            }
        }
        return View(model); 
    }

Can anyone help?

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T10:50:56+00:00Added an answer on May 30, 2026 at 10:50 am

    The problem is on the following line in your view:

    @Html.CheckBoxFor(modelItem => item.PreferenceBool)
    

    I see people writing the following lambda expression modelItem => item.SomeProperty in their views very often and asking why the model binder doesn’t correctly bind collection properties on their view models.

    This won’t generate proper name for the checkbox so that the default model binder is able to recreate the Settings collection. I would recommend you reading the following blog post to better understand the correct format that the model binder expects.

    Try like this:

    @model StartStop.ServiceResources.UserSettings
    @using (Html.BeginForm("ManageAccount", "Account", FormMethod.Post))
    {
        <table class="tbl" cellspacing="0">
            <tr>
                <th>Preference</th>
                <th>Setting</th>
           </tr>
    
           @if (Model != null)
           {
               for (var i = 0; i < Model.Settings.Count; i++)
               {
                   <tr>
                       <td>@Model.Settings[i].PreferenceName</td>
                       <td>
                           @if (Model.Settings[i].PreferenceType == 2)
                           {
                               @Html.CheckBoxFor(x => x.Settings[i].PreferenceBool)
                           }
                        </td>
                    </tr>
               }
           }
        </table>
    
        <input type="submit" value="Save Changes" class="action medium" />
    }
    

    This being said, I would recommend you using editor templates, like so:

    @using (Html.BeginForm("ManageAccount","Account", FormMethod.Post))
    {
        <table class="tbl" cellspacing="0">
            <tr>
                <th>Preference</th>
                <th>Setting</th>
           </tr>
    
           @if (Model != null)
           {
               @Html.EditorFor(x => x.Settings)
           }
        </table>
    
        <input type="submit" value="Save Changes" class="action medium" />
    }
    

    and then define a custom editor template which will automatcially be rendered for each element of the Settings collection (~/Views/Shared/EditorTemplates/Setting.cshtml):

    @model StartStop.ServiceResources.Setting
    <tr>
        <td>@Model.PreferenceName</td>
        <td>
             @if (Model.PreferenceType == 2)
             {
                 @Html.CheckBoxFor(x => x.PreferenceBool)
             }
         </td>
     </tr>
    

    Also the only input field that I can see in this form is the checkbox which is bound to the PreferenceBool property on your model. So inside your POST controller action you will get the Settings list property initialized but don’t expect to find any values for the other properties in this Setting class unless of course you include input fields for them in the form (and more precisely in the editor template that I have shown).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having a strange problem which I don't understand. I have the following
I have a problem which I don't know how to fix. It has to
I have a problem which I cant seem to find answer to through searches
I have a very simple problem which requires a very quick and simple solution
It looks like a problem which could have simple solution, but I haven't found
I have a problem which I'm pretty sure that I can't solve without going
So I have this problem, which I don't see how it's happening. Basically I
I have this weird problem which I don't remember to ever had in XCode
I have a problem with nested routes which don't display correct url. I use
I'm new to PHP, and I have stumble on the problem which I don't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.