Click here to Skip to main content
Rate this: bad
good
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
Edited 12-Apr-11 22:16pm
Peace ON10.5K
v3
Rate this: bad
good
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>
  Permalink  
Rate this: bad
good
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;}
}</string>
Then in view you just loop through names and create checkboxes.
<% foreach(string checkname in Model.checkboxes)
{ %>
<%: Html.CheckBox(checkname, false) %>
<% } %>
 

HTH
  Permalink  
v2

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

  Print Answers RSS
0 George Jonsson 175
1 Kornfeld Eliyahu Peter 169
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 13 Apr 2011
Copyright © CodeProject, 1999-2014
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