Home >Backend Development >C++ >How to Pass and Retrieve a List of Checkbox Selections Between a View and Controller in ASP.NET MVC?

How to Pass and Retrieve a List of Checkbox Selections Between a View and Controller in ASP.NET MVC?

Barbara Streisand
Barbara StreisandOriginal
2025-01-31 17:41:11291browse

How to Pass and Retrieve a List of Checkbox Selections Between a View and Controller in ASP.NET MVC?

Pass and retrieve the check box selection list between the viewing and the controller in the ASP.NET MVC.

In Web development, we often encounter the required display of the check box list in the view, allowing users to choose some check boxes, and then submit these options to the controller for processing. However, if we handle correlations and non -associated items at the same time, it may be difficult to map these options to models.

Question:

The web form uses the HTML input element to indicate the form field, including the check box. By default, the selected checkbox will not return its value to the controller. This may cause problems when we want to capture all selected values ​​(associations and non -associations) in the view model.

Solution:

In order to overcome this limit, we can use a structured method that depends on a strong type HTML helpor. By using these helpers, we can ensure the correct model binding, and combine the advantages of automatic release values ​​for selecting and not selected check boxes. Create view model:

In the above scene, we assume that there is more relationship between users and projects. In order to represent this in the view, we can define the following view model:

Filling the charging view model:

In the Get operation method of the view, we can use all items (including associated and non -associated items) to fill in the view model, and set the ISSELECTED property according to the actual association.
<code class="language-csharp">public class ItemViewModel
{
    public int ID { get; set; }
    public string Name { get; set; }
    public bool IsSelected { get; set; } // 指示此项目是否已选中
}</code>

Show the check box in the view:

In our view, we can use the HTML helper to generate the check box and display the project name:

<code class="language-csharp">// GET 操作方法中的代码
var model = new ItemViewModel();
// ... 在此处填充模型 ...
return View(model);</code>

Receive selection in the controller:

In the post operation method, we can bind the form field to our view model, and the ISSELECTED property will automatically reflect the status of the check box (selected and uns selected).

<code class="language-html">// Razor 视图中的代码
@model ItemViewModel
@for (int i = 0; i < Model.Items.Count; i++)
{
    @Html.HiddenFor(m => m.Items[i].ID) // 隐藏输入以传递项目 ID
    @Html.CheckBoxFor(m => m.Items[i].IsSelected) // 用于选择项目的复选框
    @Html.LabelFor(m => m.Items[i].IsSelected, Model.Items[i].Name) // 复选框的标签
}</code>
Through this method, we can effectively transmit and capture the list of the selection check box, regardless of its previous association state.

The above is the detailed content of How to Pass and Retrieve a List of Checkbox Selections Between a View and Controller in ASP.NET MVC?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn