Click here to Skip to main content
12,823,725 members (65,392 online)
Rate this:
Please Sign up or sign in to vote.
See more: ASP.NET MVC
how to add a dynamic checkbox using controller class in MVC Application?
Posted 11-Apr-11 0:00am
Updated 12-Apr-11 22:16pm
Peace ON10.5K
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Hi try this,

I have added Check box in MVC Application like this on ASPX page;
<%= Html.CheckBox("ClientList", false, new { id = "checkBoxID" })%> <span>Search OLD Client, only</span>
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

There are two ways to achieve your functionality.

1. You can build your model with string property and add <input type="checkbox"></input> controls with html stuff you need and then render it from model in view.
public class checkboxes
  public string checks{get; set;}

Then you need pass this model from controller to view and access it like following.
<%: Model.checkboxes %>

In that way it will render as many as checkboxes as you want.

2. Second method is related to first but with some difference. In this method you need to pass model with only some parameters of checkboxes not the html code. And from those parameters we are building checkbox control on the view.

public class checkboxes
  public List<string> checksname{get; set;}

Then in view you just loop through names and create checkboxes.
<% foreach(string checkname in Model.checkboxes)
{ %>


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170308.1 | Last Updated 13 Apr 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100