Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to add a dynamic checkbox using controller class in MVC Application?
Posted
Updated 12-Apr-11 21:16pm
v3

Hi try this,

XML
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>
 
Share this answer
 
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
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900