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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:17:45+00:00 2026-05-27T19:17:45+00:00

I am working on a ASP.NET MVC3 code to bind the CheckBoxes using the

  • 0

I am working on a ASP.NET MVC3 code to bind the CheckBoxes using the data from the column of my SQL Database present in App_Data directory.

My table SysUser3 contains two columns with values as follows:

 ProductName    ||        ProductId

  Pencil                    1
  Eraser                    2  
  Pen                       3  

Model:

public class StoreModel
{
    public List<ProductModel> Products { get; set; }
}

public class ProductModel
{
    public string ProductName { get; set; }
    public bool Selected { get; set; }
}

Controller:

    [HttpGet]
    public ActionResult CheckView()
    {
        var model = new StoreModel
        {
            Products = m.GetCheckBoxes()
        };

        return View(model);
    }

//GetCheckBoxes method()

    public IList<ProductModel> GetCheckBoxes()
    {
        IList<ProductModel> items = new List<ProductModel>();
        using (SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|DeveloperReport.mdf;User Instance=true"))
        {
            con.Open();

            string cmdString = "SELECT ProductName FROM SysUser3";
            using (SqlCommand cmd = new SqlCommand(cmdString, con))
            {
                using (SqlDataReader dataRead = cmd.ExecuteReader())
                {
                    while (dataRead.Read())
                    {
                        items.Add(new ProductModel
                        {

                            Text = dataRead["ProductName"].ToString()
                        });
                    }
                }
            }
        }
        return items;
    }

View:

     @using (Html.BeginForm())
     {

      <div>
        @Html.HiddenFor(x => x.ProductName)
        @Html.CheckBoxFor(x => x.Selected)
        @Html.LabelFor(x => x.Selected, Model.ProductName) 
      </div>

      }

However, my code isn’t working fine and I am not able to see the binding taking place.
I just get a empty checkbox when I run the code.

Can someone tell me what am I doing wrong

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-27T19:17:46+00:00Added an answer on May 27, 2026 at 7:17 pm

    In your DAL you seem to be defining the item variable to be an List<ProductModel>() and inside the while clause you seem to be adding elements of type RoleModel to this list assigning only the Text property and not the Selected property which is what the checkbox is bound to. You seem to be selecting only the ProductName (SELECT ProductName FROM SysUser3).

    There doesn’t seem to be a Selected boolean column in your table, so you cannot populate properly this property and thus the generated checkbox in the view will never be checked.

    I guess you will have to rethink your database design. But that’s another topic.

    As far as ASP.NET MVC is concerned, as long as you provide a valid view model to the view:

    public ActionResult CheckView()
    {
        var model = new StoreModel
        {
            Products = new[]
            {
                 new ProductModel { ProductName = "product 1", Selected = true },
                 new ProductModel { ProductName = "product 2", Selected = false },
                 new ProductModel { ProductName = "product 3", Selected = true },
            }.ToList()
        };
    
        return View(model);
    }
    

    no matter where this data comes from, the corresponding checkboxes in the view will be properly bound:

    @model StoreModel
    @using (Html.BeginForm())
    {
        @Html.EditorFor(x => x.Products)
        <button type="submit">OK</button>
    }
    

    and in the corresponding editor template (~/Views/Shared/EditorTemplates/ProductModel.cshtml) which will be rendered for each element of the Products collection of your view model:

    @model ProductModel
    <div>
        @Html.HiddenFor(x => x.ProductName)
        @Html.CheckBoxFor(x => x.Selected)
        @Html.LabelFor(x => x.Selected, Model.ProductName) 
    </div>
    

    And then obviously you will have the corresponding POST action which will take your view model as argument and call the underlying DAL to do some processing:

    [HttpPost]
    public ActionResult CheckView(StoreModel model)
    {
        // the model.Products collection will be properly bound here
        // with the values that the user selected in the form
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on asp .net mvc3. My database is SQL Server and I'm
With ASP.NET MVC3 I'm using Jquery/Ajax to post data from a form to a
I'm working with ASP.NET MVC3 using EF and Code First. I'm writing a simple
I'm using ASP.NET MVC 3 code-first and I have added validation data annotations to
Here is my dilemma, I am working on some SaaS using ASP.net MVC3 and
I'm working on an ASP.NET MVC app (using MVC3 RC2). Say I have 2
We have some files stored in sql database. On an ASP.NET MVC3 form, we
I am working on asp.net MVC 3 project. I am using EF 4.1 code
i am working on asp.net using C# and using jquery datepicker my question is,
I am working on ASP.NET MVC3. I have a partial view RestaurantAdminNavigation.ascx which i

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.