简体   繁体   English

如何将复选框的值传递给MVC5中的ActionResult?

[英]How to pass the values of the checkboxes to ActionResult in MVC5?

I have an MVC5 app, and in my view, I have a code like this: 我有一个MVC5应用,在我看来,我有这样的代码:

<div class="form-group">
    @Html.LabelFor(model => model.CategoryID, "Category", htmlAttributes: new { @class = "control-label col-md-3" })
    <div class="col-md-9">
        @Html.DropDownList("CategoryID", null, "Please select a category", htmlAttributes: new { @class = "form-control" })
        @Html.ValidationMessageFor(model => model.CategoryID, "", new { @class = "text-danger" })
    </div>
</div>
<div id="subcatsform" class="form-group" style="visibility:hidden;">
    @Html.LabelFor(m => m.Subcategories, new { @class = "col-md-3 control-label" })
    <div id="subcats" class="col-md-9">

    </div>
</div>

Then, I have a JavaScript code which populates the subcategories according to the previously selected category. 然后,我有一个JavaScript代码,该代码根据先前选择的类别填充子类别。 I achieve it with the following code segment: 我通过以下代码段实现它:

$(function () {
    $("#CategoryID").on("change", function () {
        var selected_option = $("#CategoryID option").filter(":selected").text();
        $("#subcats").empty();

        if (selected_option != "Please select a category") {
            $("#subcatsform").css("visibility", "visible");

            $.ajax({
                url: "/Home/GetSubcategories",
                dataType: "json",
                data: {
                    cid: $("#CategoryID").val(),
                },
                success: function (data) {
                    $.each(data, function (key, value) {
                        var checkbox = '<input type="checkbox" name="subcategory" value="' + value.id + '"> ' + value.value + '<br />';
                        $("#subcats").append(checkbox);
                    }); 
                }
            });
        }
        else
        {
            $("#subcatsform").css("visibility", "hidden");
        }
    });
});

The point is that, until there everything works fine, and for each selected category, I get the list of subcategories correctly and create checkboxes with each one of them, where the name attribute as you can see is equal to subcategory and the value attribute is equal to the actual id of the subcategory. 关键是,直到一切正常为止,并且对于每个选定的类别,我都正确地获得了子类别的列表,并创建了每个复选框的复选框,其中可以看到的name属性等于subcategory ,而value属性为等于子类别的实际ID。

What I want to do next, is that I want to get the value attributes of the checked checkboxes (meaning selected subcategories), and pass that to my controller. 接下来,我想获取选中的复选框的value属性(意味着选中的子类别),并将其传递给我的控制器。 My ActionResult is defined like this: 我的ActionResult定义如下:

public async Task<ActionResult> RegisterBusiness(BusinessViewModel model)

Inside the BusinessViewModel , I have a property like this: BusinessViewModel内部,我有一个像这样的属性:

public virtual ICollection<Subcategory> Subcategories { get; set; }

And, finally my Subcategory model is defined like this: 最后,我的Subcategory模型的定义如下:

public class Subcategory
{
    public int SubcategoryID { get; set; }

    public string SubcategoryName { get; set; }

    public string SubcategoryDescription { get; set; }

    [ForeignKey("Category")]
    [Display(Name = "Category")]
    public int CategoryID { get; set; }

    public virtual Category Category { get; set; }

    [NotMapped]
    public bool IsSelected { get; set; }
}

If someone can help me pass the values of the selected checkboxes into my RegisterBusiness ActionResult, and then store them inside my ICollection<Subcategory> Subcategories property, I would be glad. 如果有人可以帮助我将所选复选框的值传递到RegisterBusiness ActionResult中,然后将其存储在我的ICollection<Subcategory> Subcategories属性中,我将非常高兴。

Change your RegisterBusiness Action to take a int array named subcategory 更改您的RegisterBusiness Action以采用名为subcategory的int数组

public async Task<ActionResult> RegisterBusiness(BusinessViewModel model, int[] subcategory)

The array will automatically catch the selected checkboxes' values. 数组将自动捕获所选复选框的值。

Now, at postback create ICollection<Subcategory> from string[] subcategory and assign to Subcategories. 现在,在回发时,从string[] subcategory ICollection<Subcategory>创建ICollection<Subcategory>并将其分配给Subcategories。

using System.Linq;
...
...
ICollection<Subcategory> Subcategories = subcategory.Select(x => new Subcategory { SubcategoryID = x}).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM