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 6156753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:39:54+00:00 2026-05-23T20:39:54+00:00

When passing a view model (as below) to a view, how can I ensure

  • 0

When passing a view model (as below) to a view, how can I ensure that the checkboxes I’m creating (mapped to item “Product” in here) get passed back to the controller?

I’ve included my view model and “post” product controller below.

Unfortunately, when posted back to the controller, “Products” is null.

namespace MyProject.Models
{
    public class ChartViewModel
    {
        public Chart ChartItem { get; set; }
        public IEnumerable<Product> Products { get; set; }
    }
}

Controller:

    [Authorize]
    [ValidateInput(false)]
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Edit(ChartViewModel objChartViewModel)
    {

        if (!TryUpdateModel(objChartViewModel))
        {
            return View(objChartViewModel);
        }
        else
        {

        } return View("Details", objChartViewModel);

    }

How the checkboxes are added to my view, mapped to the “Product” object within my view model:

@{
   foreach (MyProject.Models.Product objProduct in Model.Products)
 {
            @Html.CheckBox("product" + objProduct.Id, Model.ChartItem.ChartProducts.Select(t => t.ProductId).Contains(objProduct.Id));
            @String.Format("{0} {1}", objProduct.Manufacturer.Name, objProduct.Name);<br />
 }
}
  • 1 1 Answer
  • 0 Views
  • 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-23T20:39:55+00:00Added an answer on May 23, 2026 at 8:39 pm

    You can send your lists (IEnumerable<T>) down to the view but when they don’t come back up to the controller. The only properties of your ViewModel that have values are those with exactly matching items in the forms collection. So add a properties to your ViewModel like SelectedProductID.

    This sets up a drop down list that sends its selected value back to the controller:

    <div class="editor-field"> 
        @Html.DropDownListFor(m => m.SelectedEmployeeID,
            new SelectList(Model.EmployeeList, "EmployeeID", "EmployeeName"), "--Please select an Employee--")
        @Html.ValidationMessageFor(model => model.SelectedEmployeeID)
    </div>
    

    Notice: the property being set is SelectedEmployeeID but this comes from the EmployeeList.

    In your case the values many be in the collection (only “checked” checkboxes get sent in a post) so you could do:

    string value = collection["myProductID"];
    

    if its there its checked.

    sorry for the messiness, this was in a bit of a rush. See this for more info:
    MVC 3 form post and persisting model data

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

Sidebar

Related Questions

I'm building a site in MVC and the View Model I am passing to
I have a property on my view model that is a custom class with
I have generated a strongly-type view from a model class that I have created
Given the backbone view function below, what is the correct way of passing this
I'm passing a complex object as a Model to the View as but when
I'm passing data between my controller and my view using a ViewModel class. When
Im having problems displaying records to my view when passing viewdata to a user
Is it ok to instantiate an object in a View before passing it to
How do I go about passing two sets of Data to a view in
I am outputting a students logged problems by passing the student into the view

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.