Asp.net mvc 在MVC中获取多个选中的复选框
我有一个ProductController,它由Create方法组成 我的模型:Asp.net mvc 在MVC中获取多个选中的复选框,asp.net-mvc,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 4,我有一个ProductController,它由Create方法组成 我的模型: public class ProductEntry { public Crescent.LinqModel.Product Products { get; set; } public ProductSKU SKUs { get; set; } public List<SelectListItem> pColors { get; set; } public Product
public class ProductEntry
{
public Crescent.LinqModel.Product Products { get; set; }
public ProductSKU SKUs { get; set; }
public List<SelectListItem> pColors { get; set; }
public ProductEntry()
{
pColors = new List<SelectListItem>();
}
}
创建视图:
@model CrescentAdmin.Models.ProductEntry
用于填写复选框列表的代码:
<tr>
<td>
Product Colors
</td>
<td>
@if (Model.pColors != null && Model.pColors.Count > 0)
{
for (int i = 0; i < Model.pColors.Count; i++)
{
//if (Model.pColors[i])
//{
<input type="checkbox" value="@Model.pColors[i].Value" id="@Model.pColors[i].Value"/> @Model.pColors[i].Text <br />
@Html.HiddenFor(m => Model.pColors[i].Value);
@Html.HiddenFor(m => Model.pColors[i].Text);
@Html.HiddenFor(m => Model.pColors[i].Selected);
//}
//else
//{
// <input type="checkbox" value="@Model.pColors[i].Value" /> @Model.productColors[i].Name <br />
//}
}
}
@Html.ValidationMessageFor(model => model.SKUs.ProductColors)
</td>
</tr>
产品颜色
@如果(Model.pColors!=null&&Model.pColors.Count>0)
{
对于(int i=0;i
@Html.HiddenFor(m=>Model.pColors[i].Value);
@Html.HiddenFor(m=>Model.pColors[i].Text);
@Html.HiddenFor(m=>Model.pColors[i].Selected);
//}
//否则
//{
//@Model.productColors[i].Name
//}
}
}
@Html.ValidationMessageFor(model=>model.SKUs.ProductColor)
我已经尝试过这个代码,但是没有成功
需要获取选中的复选框吗??请帮助尝试以下操作:
@Html.HiddenFor(m => Model.pColors[i].Value);
@Html.HiddenFor(m => Model.pColors[i].Text);
@Html.CheckBoxFor(m => Model.pColors[i].Selected);
我使用这个扩展:
public static MvcHtmlString CheckBoxListFor<TModel, TProperty>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, IEnumerable<TProperty>>> expression,
IEnumerable<SelectListItem> multiSelectList,
Object htmlAttributes = null)
{
// Derive property name for checkbox name
var body = expression.Body as MemberExpression;
if (body == null)
return null;
String propertyName = body.Member.Name;
// Get currently select values from the ViewData model
IEnumerable<TProperty> list = expression.Compile().Invoke(htmlHelper.ViewData.Model);
// Convert selected value list to a List<String> for easy manipulation
var selectedValues = new List<String>();
if (list != null)
selectedValues = new List<TProperty>(list).ConvertAll(i => i.ToString());
// Create div
var ulTag = new TagBuilder("ul");
ulTag.AddCssClass("checkBoxList");
ulTag.MergeAttributes(new RouteValueDictionary(htmlAttributes), true);
// Add checkboxes
foreach (var item in multiSelectList)
{
ulTag.InnerHtml += String.Format(
"<li><input type=\"checkbox\" name=\"{0}\" id=\"{0}_{1}\" value=\"{1}\" {2} /><label for=\"{0}_{1}\">{3}</label></li>",
propertyName,
item.Value,
selectedValues.Contains(item.Value) ? "checked=\"checked\"" : "",
item.Text);
}
return MvcHtmlString.Create(ulTag.ToString());
}
publicstaticmvchtmlstring CheckBoxListFor(
这个HtmlHelper HtmlHelper,
表情表情,
IEnumerable多选列表,
对象htmlAttributes=null)
{
//为复选框名称派生属性名称
var body=expression.body作为MemberExpression;
if(body==null)
返回null;
字符串propertyName=body.Member.Name;
//获取当前从ViewData模型中选择的值
IEnumerable list=expression.Compile().Invoke(htmlHelper.ViewData.Model);
//将选定值列表转换为列表以便于操作
var selectedValues=新列表();
如果(列表!=null)
selectedValues=新列表(List.ConvertAll)(i=>i.ToString());
//创建div
var ulTag=新标记生成器(“ul”);
ulTag.AddCssClass(“复选框列表”);
ulTag.MergeAttributes(新的RouteValueDictionary(htmlAttributes),true);
//添加复选框
foreach(多选列表中的变量项)
{
ulTag.InnerHtml+=String.Format(
“{3} ”,
propertyName,
项目.价值,
selectedValues.Contains(item.Value)?“checked=”checked\:“”,
项目(案文);
}
返回MvcHtmlString.Create(ulTag.ToString());
}
在处理复选框时,我通常使用下面的方法检查它是否对您有帮助
型号:
namespace GateApplication.Models
{
public class Gate
{
public string PreprationRequired { get; set; }
public List<CheckBoxes> lstPreprationRequired{ get; set; }
public string[] CategoryIds { get; set; }
}
public class CheckBoxes
{
public int ID { get; set; }
public string Value { get; set; }
public string Text { get; set; }
public bool Checked { get; set; }
}
}
@foreach (var item in Model.lstPreprationRequired)
{
<input type="checkbox" id="@item.Value" name="CategoryIds" value="@item.Text"/>
<label for="optionId">@item.Text</label>
<br />
}
@foreach (var item in Model.lstPreprationRequired)
{
<input type="checkbox" id="@item.Value" name="CategoryIds" value="@item.Text"
@foreach (var c in Model.CategoryIds)
{
if(c == item.Value)
{
<text> checked="checked"</text>
}
}/>
<label for="optionId">@item.Text></label>
}
现在您的数据库中有以下类型的字符串
安全、电力、通道
现在获取所选值并显示视图
public ActionResult Edit(int id)
{
List<CheckBoxes> lstchk = new List<CheckBoxes>()
{
new CheckBoxes {Text="coduit", Value="coduit" },
new CheckBoxes {Text="safety", Value="safety" },
new CheckBoxes {Text="power", Value="power" },
new CheckBoxes {Text="access", Value="access" }
};
var model = new Gate
{
lstPreprationRequired =lstchk,
CategoryIds = "safety,power,access".Split(',')//here get your comma separated list from database and assign it to the CategoryIds string array, i have used sample text for the values
};
return View(model);
}
public ActionResult编辑(int-id)
{
List lstchk=新列表()
{
新复选框{Text=“coduit”,Value=“coduit”},
新复选框{Text=“safety”,Value=“safety”},
新复选框{Text=“power”,Value=“power”},
新复选框{Text=“access”,Value=“access”}
};
var模型=新闸门
{
LstPrepositionRequired=lstchk,
CategoryIds=“safety,power,access”.Split(',')//这里从数据库中获取逗号分隔的列表,并将其分配给CategoryIds字符串数组,我使用了示例文本作为值
};
返回视图(模型);
}
查看:
namespace GateApplication.Models
{
public class Gate
{
public string PreprationRequired { get; set; }
public List<CheckBoxes> lstPreprationRequired{ get; set; }
public string[] CategoryIds { get; set; }
}
public class CheckBoxes
{
public int ID { get; set; }
public string Value { get; set; }
public string Text { get; set; }
public bool Checked { get; set; }
}
}
@foreach (var item in Model.lstPreprationRequired)
{
<input type="checkbox" id="@item.Value" name="CategoryIds" value="@item.Text"/>
<label for="optionId">@item.Text</label>
<br />
}
@foreach (var item in Model.lstPreprationRequired)
{
<input type="checkbox" id="@item.Value" name="CategoryIds" value="@item.Text"
@foreach (var c in Model.CategoryIds)
{
if(c == item.Value)
{
<text> checked="checked"</text>
}
}/>
<label for="optionId">@item.Text></label>
}
@foreach(Model.lstpprepositionrequired中的变量项)
{
@项目.文本>
}
如果这对您没有帮助,请告诉我。我也有类似您的场景,我通过引用此链接使用EditorFor实现了此场景“” 为此,您需要创建一个EditorTemplate文件夹,并需要添加具有相同模型名称的视图。查看下面的代码
@model EditorForSample.Models.ProductViewModel
<tr>
<td class="text-nowrap">@Html.CheckBoxFor(m => m.Selected, new {@class="chkSelect"}) </td>
<td colspan="3">
@Model.Name
@Html.HiddenFor(m => m.Name)
</td>
<td>
@Model.Price
@Html.HiddenFor(m => m.Price)
</td>
.......
<div class="col-md-12">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th><input type="checkbox" id="chkSelectAll" /></th>
<th colspan="3">
Product Name
</th>
<th>
Cost
</th>
</tr>
</thead>
<tbody>
@Html.EditorFor(m => m.ProductsList)
</tbody>
</table>
</div>
.........
@model EditorForSample.Models.ProductViewModel
@CheckBoxFor(m=>m.Selected,new{@class=“chkSelect”})
@型号.名称
@Html.HiddenFor(m=>m.Name)
@型号、价格
@Html.HiddenFor(m=>m.Price)
模型可以如下所示
public class ProductPageViewModel
{
public string Message { get; set; }
public List<ProductViewModel> ProductsList { get; set; }
}
public class ProductViewModel
{
public bool Selected{ get; set; }
public string Name { get; set; }
public int Price { get; set; }
}
公共类ProductPageViewModel
{
公共字符串消息{get;set;}
公共列表产品列表{get;set;}
}
公共类ProductViewModel
{
已选择公共布尔值{get;set;}
公共字符串名称{get;set;}
公共整数价格{get;set;}
}
在父视图中,您可以像下面这样编写html
@model EditorForSample.Models.ProductViewModel
<tr>
<td class="text-nowrap">@Html.CheckBoxFor(m => m.Selected, new {@class="chkSelect"}) </td>
<td colspan="3">
@Model.Name
@Html.HiddenFor(m => m.Name)
</td>
<td>
@Model.Price
@Html.HiddenFor(m => m.Price)
</td>
.......
<div class="col-md-12">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th><input type="checkbox" id="chkSelectAll" /></th>
<th colspan="3">
Product Name
</th>
<th>
Cost
</th>
</tr>
</thead>
<tbody>
@Html.EditorFor(m => m.ProductsList)
</tbody>
</table>
</div>
.........
。。。。。。。
品名
成本
@EditorFor(m=>m.ProductsList)
.........
上面的链接也有示例项目。我知道这个问题的答案已经被接受了,但只是分享一下。它可能会帮助某人。谢谢:-)
$(“#btndownloading”)。单击(函数(e){
var ckebox_value=$(“输入[name=cmmdLabels]:选中”).map(函数(){
如果($(this).val()!=0){
返回$(this.val();
}
}).get();
var ckeck_box_length=ckebox_value.length;
如果(ckeck\U box\U长度!=0){
$.ajax({
url:'@url.Action(“加载文件”,“加载”),
类型:“POST”,
数据:JSON.stringify({selectionvalue\u ck:ckebox\u value}),
contentType:“应用程序/json;字符集=utf-8”,
成功:功能(数据){
},
}
});
}
否则{
警报(“请至少选择