在MVC应用程序中添加复选框 [英] Add a Checkbox in MVC Application

查看:103
本文介绍了在MVC应用程序中添加复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在MVC应用程序中使用控制器类添加动态复选框?

how to add a dynamic checkbox using controller class in MVC Application?

推荐答案

尝试一下,

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>


有两种方法可以实现您的功能.

1.您可以使用字符串属性构建模型,并使用所需的html内容添加<input type="checkbox"></input>控件,然后从视图中的模型进行呈现.
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 %>


这样,它将根据需要呈现尽可能多的复选框.

2.第二种方法与第一种有关,但有所不同.在这种方法中,您只需要传递带有复选框某些参数而不是html代码的模型.然后根据这些参数,在视图上构建复选框控件.


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




HTH


这篇关于在MVC应用程序中添加复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆