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

  • Home
  • SEARCH
  • 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 4247680
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:08:29+00:00 2026-05-21T04:08:29+00:00

I have public class ViewModel1 { // some properties here public List<ViewModel2> ViewModel2 {get;

  • 0

I have

public class ViewModel1
{
   // some properties here
   public List<ViewModel2> ViewModel2 {get; set;}
}

public class ViewModel2
{
   public string A {get; set;}
   public string B {get; set;}
}

// view

<table>
  <thead>
     <tr> a </tr>
     <tr> b </tr>
  </thead>
   <tbody>
      // need to generate all ViewModel2 stuff in table row cell
   </tbody>
</table>

My problem is that if I put a foreach loop like this

@foreach(var m in Model.ViewModel2)
{
   <tr>
     <td>@Html.CheckBoxFor(x => m.A) </td>
     <td>@Html.CheckBoxFor(x => m.B) </td>
   </tr>
}

The problem with this each of these checkboxes will have the same id’s and same name attribute what of course is not valid html.

How can I make them have either no id or a unquie id and unique name attribute?

I tried to do partial views and display templates both give me the same problem.

The only thing I can think of is

 int i = 0
    @foreach(var m in Model.ViewModel2)
    {
       <tr>
         <td>@Html.CheckBoxFor(x => m.A, new {@name = m.a + i}) </td>
         <td>@Html.CheckBoxFor(x => m.B) </td>
       </tr>
    }

But I think that is a horrible way to do it.

  • 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-21T04:08:30+00:00Added an answer on May 21, 2026 at 4:08 am

    Use a for loop not a foreach loop.

    @for(int i = 0; i < Model.ViewModel2.Length; i++)
    {
       <tr>
         <td>@Html.CheckBoxFor(x => x.ViewModel2[i].A) </td>
         <td>@Html.CheckBoxFor(x => x.ViewModel2[i].B) </td>
       </tr>
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following ViewModel public class RecommendationModel { public List<CheckBoxItem> CheckBoxList { get;
I have a viewmodel. public class RegistrationViewModel { public RegisterModel Register { get; set;
I have built a base class for my view model(s). Here is some of
I have a simple ViewModel class like public class TestViewModel { public String Information
I have a viewmodel class for a create view that consists of 2 properties
Here's the situation, I have a list of about 20 properties (called Attributes) that
I have a ViewModel : public class VM { public ObservableCollction<PersonRole> PersonRoles { get;
I have the following nested viewmodel class... public class CustomerModel { public string name;
I am trying to pass some list to my view through ViewModel. I have
I have a class that roughly looks like this: public class ViewModel { public

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.