首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在控制器中获取选定的CheckBox值

在控制器中获取选定的CheckBox值
EN

Stack Overflow用户
提问于 2017-07-10 22:38:27
回答 2查看 1.7K关注 0票数 1

我有一个宿舍添加页面,这个宿舍可以有功能,所以我想使用CheckBox列表。

有一个宿舍可以拥有的所有功能的列表。

代码语言:javascript
复制
public class DormFeatureModel
{
    [Key]
    public int FeatureID { get; set; }
    public string FeatureName { get; set; }

    public List<DormHasFeatureModel> DormHasFeature { get; set; }


}

这里也有一些宿舍所具备的功能。

代码语言:javascript
复制
public class DormHasFeatureModel
{

    [Key]
    public int HasFeatureID { get; set; }
    [Required]
    public int FeatureID { get; set; }
    [Required]
    public int DormID { get; set; }

    public virtual DormModel Dorm { get; set; }
    public virtual DormFeatureModel DormFeature { get; set; }


}

我可以在razor中获得功能列表作为复选框,但我无法获得选中的复选框id列表(所以,FeatureID)

如何在控制器中获取列表?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-07-10 23:02:53

首先,添加一个将Checked布尔值与FeatureId关联起来的ViewModel。

代码语言:javascript
复制
public class SelectedFeatureViewModel {
     public bool Checked { get; set; }       // to be set by user
     public int FeatureID { get; set; }      // to be populated by GET action
     public string FeatureName { get; set; } // to be populated by GET action
} 

GET操作创建主ViewModel并初始化可用特性列表(DormOptions)。

代码语言:javascript
复制
public class CreateDormViewModel {

    // used to render the checkboxes, to be initialized in GET controller action
    // also used to bind the checked values back to the controller for POST action
    public ICollection<SelectedFeatureViewModel> DormOptions { get; set; }
}

在剃刀标记中,将复选框绑定到DormOptions集合:

代码语言:javascript
复制
@model CreateDormViewModel 

@using (Html.BeginForm("CreateDorm", "DormAdministration", FormMethod.Post)) {

    // use for loop so modelbinding to collection works
    @for (var i = 0; i < Model.DormOptions.Count; i++) { 
        <label>@Model.DormOptions[i].FeatureName</label>
        @Html.CheckBoxFor(m => m.DormOptions[i].Checked)
        // also post back FeatureId so you can access it in the controller
        @Html.HiddenFor(m => m.DormOptions[i].FeatureID)
        // post back any additional properties that you need to access in the controller
        // or need in order to redraw the view in an error case
        @Html.HiddenFor(m => m.DormOptions[i].FeatureName)
    }
}

CreateDorm POST操作中,复选框值被绑定到您在CheckBoxFor lambda中提供的ViewModel属性,即DormOptions集合中的Checked属性。

代码语言:javascript
复制
[HttpPost]
public ActionResult CreateDorm(CreateDormViewModel postData) {

    var selectedFeatureIds = new List<int>();
    foreach (var option in postData.DormOptions) {
        if (option.Checked) {
            selectedFeatureIds.Add(option.FeatureID);
        }
    }
    // ...
}
票数 1
EN

Stack Overflow用户

发布于 2017-07-11 10:35:05

你可以通过使用复选框的名称来获得列表,假设你的复选框的名称是chklstfeatureid,那么在控制器中你可以获得如下列表

代码语言:javascript
复制
public actionresult createdorm(list<int> chklstfeatureid)
{

}

谢谢

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45014951

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档