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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:23:41+00:00 2026-06-10T14:23:41+00:00

I have 2 lists of different types – List List The db tables are

  • 0

I have 2 lists of different types –

List
List

The db tables are structured as follows

Product
-----------
ProductID - PK
Name
Description
Price
OwnerID - FK
...

SelectedProducts
---------------
SelectedProductID - PK
ProductID - FK
Active

So I have a page where the user is displayed a list of products, they can select some or all of the products. I am now trying to create an edit page which displays a full list of products, but checks the checkboxes for products already selected.

I have tried the following approaches, however both give me duplicates of some sort –

Approach 1
This approach gives me a list of products, and then duplicates the products because of the ones already selected.

    <div style="margin-left: 10px; margin-top: 10px;">
    <% foreach (var product in Model.Products) { %>
        <% foreach (var p in Model.SelectedProducts)
           { %>
           <% if (p.ProductID == product.ProductID)
              { %>
                <div style="float: left; line-height: 18px; padding: 2px; margin: 2px; vertical-align: middle;
                        border: 1px solid grey; width: 282px;">
                        <input type="checkbox" name="PRODUCT_<%: product.Name %>" value="<%: ViewData["PRODUCT_" + product.ProductID] %>" checked="checked" style="vertical-align: middle; padding-left: 5px;" />
                        <%: Html.Truncate(product.Name, 35) %>
                </div>
                <% } %>
            <% } %>
            <div style="float: left; line-height: 18px; padding: 2px; margin: 2px; vertical-align: middle;
                    border: 1px solid grey; width: 282px;">
                    <input type="checkbox" name="PRODUCT_<%: product.Name %>" value="<%: ViewData["PRODUCT_" + product.ProductID] %>" style="vertical-align: middle; padding-left: 5px;" />
                    <%: Html.Truncate(product.Name, 35) %>
            </div>
        <% } %>        
    </div>

Approach 2

<div style="margin-left: 10px; margin-top: 10px;">
        <% foreach (var product in Model.Products) { %>
            <% foreach (var p in Model.SelectedProducts)
               { %>
               <% if (p.ProductID == product.ProductID)
                  { %>
        <div style="float: left; line-height: 18px; padding: 2px; margin: 2px; vertical-align: middle;
                        border: 1px solid grey; width: 282px;">
                        <input type="checkbox" name="PRODUCT_<%: product.Name %>" value="<%: ViewData["PRODUCT_" + product.ProductID] %>" style="vertical-align: middle; padding-left: 5px;" />
                        <%: Html.Truncate(product.Name, 35) %>
          </div>
        <% } else { %>
         <div style="float: left; line-height: 18px; padding: 2px; margin: 2px; vertical-align: middle; border: 1px solid grey; width: 282px;">
         <input type="checkbox" name="PRODUCT_<%: product.Name %>" value="<%: ViewData["PRODUCT_" + product.ProductID] %>" checked="checked" style="vertical-align: middle; padding-left: 5px;" />
                      <%: Html.Truncate(product.Name, 35) %>
                    </div>
                <% } %>
            <% } %>
        <% } %>        
    </div>

I know it’s just a silly logic error but I could do with a fresh pair of eyes!

  • 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-06-10T14:23:42+00:00Added an answer on June 10, 2026 at 2:23 pm

    If you remove the inner loop and instead just test each item in the outerloop against your selected products list, you should be good to go:

    <div style="margin-left: 10px; margin-top: 10px;">
            <% foreach (var product in Model.Products) { %>
            <% if (Model.SelectedProducts.Exists(x => x.ProductId == product.ProductId))
                      { %>
            <div style="float: left; line-height: 18px; padding: 2px; margin: 2px; vertical-align: middle;
                            border: 1px solid grey; width: 282px;">
                            <input type="checkbox" name="PRODUCT_<%: product.Name %>" value="<%: ViewData["PRODUCT_" + product.ProductID] %>" style="vertical-align: middle; padding-left: 5px;" />
                            <%: Html.Truncate(product.Name, 35) %>
              </div>
            <% } else { %>
             <div style="float: left; line-height: 18px; padding: 2px; margin: 2px; vertical-align: middle; border: 1px solid grey; width: 282px;">
             <input type="checkbox" name="PRODUCT_<%: product.Name %>" value="<%: ViewData["PRODUCT_" + product.ProductID] %>" checked="checked" style="vertical-align: middle; padding-left: 5px;" />
                          <%: Html.Truncate(product.Name, 35) %>
                        </div>
                    <% } %>
                <% } %>
            <% } %>        
        </div>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some models; Vocabularies (tags lists), Labels (tags) and different articles types. These
I have two different list types. I need to remove the elements from list1
My problem is this. I have lists of different numeric types, for example: QList<qreal>
I have two Generic Lists containing different types, for the sake of example, lets
Ok, so I have two lists of items which are of different types. var
I have the following scenario: Two lists of different Types which happen to contain
I have a couple of generic lists with completely different types of data. In
I have an html ordered list with differnent types e.g <ol type=a> <li>This is
I have 2 lists of different objects, that I need to join on 2
I have several different lists I want to call. They all have the same

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.