如何使用 MVC 将模型传递给选定复选框的控制器?

How to pass Models to Controller for selected checkbox using MVC?

我有一个 ASP.NET 核心 MVC 应用程序。该视图包含一个下拉列表,可以在其中选择同一类别的多个项目。当我按下“提交”时,我想要 SomeType 的完整模型(包括 Id, Name, Value, ExtraInfo)。在当前设置中,我只得到 SomeType:

Name

HomeController.cs

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using WebApp.Models;

namespace WebApp.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }

        public IActionResult Index()
        {
            var viewModel = new MainViewModel()
            {
                SomeInfo = "test",
                SomeModel = new SomeModel
                {
                    Name = "Model1",
                    SomeType1 = new List<SomeType1>
                    {
                        new SomeType1 { Id = "1", Name = "Spinach", Value = "TXT_FLD_SPINA", ExtraInfo = "something1" },
                        new SomeType1 { Id = "2", Name = "Broccoli", Value = "TXT_FLD_BRO", ExtraInfo = "something else5" },
                        new SomeType1 { Id = "3", Name = "Wheatgrass", Value = "TXT_FLD_WHE", ExtraInfo = "something else4" },
                    },
                    SomeOtherType2 = new List<SomeType1>
                    {
                        new SomeType1 { Id = "1", Name = "Apple", Value = "TXT_FLD_APPLE", ExtraInfo = "something" },
                        new SomeType1 { Id = "2", Name = "Banana", Value = "TXT_FLD_BANA", ExtraInfo = "something else" },
                        new SomeType1 { Id = "3", Name = "Tomatoes", Value = "TXT_FLD_TOM", ExtraInfo = "something else2" },
                    }
                }
            };

            return View(viewModel);
        }

        [HttpPost]
        public IActionResult Index(string search, List<SomeType1> SomeType1, string[] SomeOtherType2)
        {
            return View();
        }
    }
}

MainViewModel.cs

using System.Collections.Generic;

namespace WebApp.Models
{
    public class MainViewModel
    {
        public SomeModel SomeModel { get; set; }
        public string SomeInfo { get; set; }
    }

    public class SomeModel
    {
        public List<SomeType1> SomeType1 { get; set; }
        public List<SomeType1> SomeOtherType2 { get; set; }
        public string Name { get; set; }
    }

    public class SomeType1
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public string Value { get; set; }
        public string ExtraInfo { get; set; }
    }

}

Index.cshtml

@model MainViewModel
@{
    ViewData["Title"] = "Home Page";
}

@using (Html.BeginForm("Index", "Home"))
{
    <div class="text-center">
        <input type="text" name="search" />
        <input type="submit" value="Submit" />
    </div>

    <br />

    foreach (var item in Model.SomeModel.SomeType1)
    {
        <b>Some Type 1</b>
        <div class="checkbox">
            <label>
                <input type="checkbox"
                       name="SomeType1"
                       value="@item.Value" /> @item.Name
            </label>
        </div>
    }

    foreach (var item in Model.SomeModel.SomeOtherType2)
    {
        <b>SomeOtherType2</b>
        <div class="checkbox">
            <label>
                <input type="checkbox"
                       name="SomeOtherType2"
                       value="@item.Value" /> @item.Name
            </label>
        </div>
    }
}

根据您的描述,我建议您尝试修改视图,因为复选框的名称应该是“SomeType1[0].Value”而不是“SomeType1”。由于自动模型绑定会在绑定时检查表单名称,这就是SomeType1为null的原因。

更多详情,您可以参考以下代码:

@model MainViewModel
@{
    ViewData["Title"] = "Home Page";
}

@using (Html.BeginForm("Index", "DropDownTest"))
{
    <div class="text-center">
        <input type="text" name="search" />
        <input type="submit" value="Submit" />
    </div>

    <br />

    for (int i = 0; i < Model.SomeModel.SomeType1.Count; i++)
    {
        <b>Some Type 1</b>
        <div class="checkbox">
            <label>
                <input type="checkbox"
                       name="SomeType1[@i].Value"
                       value="@Model.SomeModel.SomeType1[i].Value" /> @Model.SomeModel.SomeType1[i].Name
                </label>
            </div>
        }

        for (int i = 0; i < Model.SomeModel.SomeOtherType2.Count; i++)
        {
            <b>SomeOtherType2</b>
            <div class="checkbox">
                <label>
                    <input type="checkbox"
                           name="SomeOtherType2"
                           value="@Model.SomeModel.SomeOtherType2[i].Value" /> @Model.SomeModel.SomeOtherType2[i]..Name
                    </label>
                </div>
            }


     
            }

结果: