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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:11:40+00:00 2026-05-22T15:11:40+00:00

So I have a view model call ProductViewModel which has a list of sites

  • 0

So I have a view model call ProductViewModel which has a list of sites where a product can be produced. I am trying to figure out how to show this on a form using a checkbox for the user to select the sites where the product can be produced. Seems straight forward, right? Well it doesn’t work like I want/need. Hoping someone can help guide me to the correct way to do this.

My classes:

 public class ProductViewModel
 {
    public List<Sites> SiteList {get; set;}   
    public string ProductName {get; set;}
    public int ProductId {get; set;}
    public User ProductOwner{get; set;}
 }

 public class Sites
 { 
    public int SiteId {get; set;} 
    public string SiteName {get; set;}
    public bool IsSelected {get; set;}    
 }

Part of my view:

@Html.LabelFor(m=>m.Sites):
@foreach (var site in Model.Sites)
{
    @Html.CheckBox("Sites", site.IsSelected, new { value = site.SiteName })
    @Html.Label(site.SiteName)
} 

When using @Html.Checkbox() I see the following output in the html from the browser:

<input checked="checked" id="Sites" name="Sites" type="checkbox" value="Miami" />
<input name="Sites" type="hidden" value="false" />

I understand the hidden field but what I really need is to get the value for the selected item. So I need to get back the list with Miami in it. I don’t need the false/true thing that the html helper seem to want to send (i.e. Miami=true)

So instead I tried this.

@for(int id=0; id < Model.Sites.Count(); id++)
{
    <input type="checkbox" id="@Model.Sites[id].SiteName" name="Sites[@id]" value="@Model.BoxingSites[id].SiteName" @(Model.Sites[id].IsSelected  ? @"checked=""checked""": "") />
    @Html.Label(Model.Sites[id].SiteName)                   
}  

And the output is:

<input type="checkbox" id="Miami" name="Sites[0]" value="Miami" checked=&quot;checked&quot; />
<label for="Miami">Miami</label>

In both of these cases I am not able to get the binder to map the form values to the Product.Sites list when posting to the action.

The action is like this:

[HttpPost]
public ActionResult Edit(ProductViewModel Product)
{
     //Does something with the form data.
}

The other values (ProductName etc…) map fine.

What am I doing wrong? I feel I am missing something as this should be easier due to how MVC simplifies so many other form handling situations.

Thanks in advance…

  • 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-22T15:11:40+00:00Added an answer on May 22, 2026 at 3:11 pm

    How about using an editor template instead of struggling with loops:

    @model ProductViewModel
    @using (Html.BeginForm())
    {
        ... some other form fields
    
        @Html.LabelFor(x => x.SiteList)
        @Html.EditorFor(x => x.SiteList)
    
        <input type="submit" value="Create" />
    }
    

    and inside the corresponding editor template ~/Views/Shared/EditorTemplates/Sites.cshtml:

    @model Sites
    <div>
        @Html.HiddenFor(x => x.SiteId)
        @Html.CheckBoxFor(x => x.IsSelected)
        @Html.LabelFor(x => x.SiteName)
    </div>
    

    Now not only that your view code is much more clean but proper names will be generated for the input fields so that the model binder will be able to bind the selected values back in the POST action.

    [HttpPost]
    public ActionResult Create(ProductViewModel model)
    {
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view model with a property Fields which is an ObservableCollection<FieldVM> .
I have a view model that contains a Product class type and an IEnumerable<
I am using Caliburn Micro and have a login view model which is displayed
I have problem to call the method from view to controller. SendVotingPanel.cshtml @model EmailTemplete.Models.SendVotingEmail
I have need of a view model that tracks changes so the user can
I have Knockout.js view model that has nested observable arrays. function ParentVM(data) { var
I have a view model ViewModel1 that has all of the proerties for my
I have a view that has 2 combo boxes (Call them ParentTypeCombo and ChildTypeCombo).
I have created a View Model in Silverlight. This View Model has an event
I have a view model that implements IValidatableObject and also has several validation attributes.

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.