【问题标题】:Getting values from CheckBoxes with composite model使用复合模型从 CheckBox 中获取值
【发布时间】:2014-01-28 02:17:06
【问题描述】:

我正在尝试获取用户选中复选框的 Id 值列表。这是模型:

using System.Collections.Generic;

namespace TestWebApplication3.Models
{
    public class TestViewModel
    {
        public IEnumerable<InnerViewModel> ModelData { get; set; }

        public class InnerViewModel
        {
            public int Id { get; set; }

            public bool Checked { get; set; }
        }
    }
}

控制器:

using System.Web.Mvc;
using TestWebApplication3.Models;

namespace TestWebApplication3.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var test = new TestViewModel();

            test.ModelData = new[]
            {
                new TestViewModel.InnerViewModel {Id = 10},
                new TestViewModel.InnerViewModel {Id = 20},
                new TestViewModel.InnerViewModel {Id = 30},
                new TestViewModel.InnerViewModel {Id = 40}
            };

            return View(test);
        }

        [HttpPost]
        public string TestAction(TestViewModel model)
        {
            string s = "";
            foreach (TestViewModel.InnerViewModel innerViewModel in model.ModelData)
            {
                if (innerViewModel.Checked)
                    s += innerViewModel.Id + " ";
            }
            return s;
        }
    }
}

还有观点:

@model TestWebApplication3.Models.TestViewModel

@using (Html.BeginForm("TestAction", "Home"))
{
    <ol>
        @foreach (var testData in Model.ModelData)
        {
            <li>
                @Html.HiddenFor(m => testData.Id)
                @Html.CheckBoxFor(m => testData.Checked)
            </li>
        }
    </ol>

    <input type="submit"/>
}

所以我将 InnerViewModel 对象列表(在 Index 操作中创建)显示为复选框。当用户提交表单时,我想以某种方式获取在 TestAction 方法中“检查”的 Id 值列表。但返回的模型始终为空。

在我正在制作的应用程序中,模型有更多属性,因此将 InnerViewModel 对象列表嵌套在 TestViewModel 中很重要。我也不想使用 MvcCheckBoxList 之类的第三方解决方案,因为在我看来对于这样一个简单的任务来说太过分了。

谁能向我解释这个工作缺少什么?

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-3 razor


    【解决方案1】:

    我稍微更改了您的代码以使其正常工作 -

    ViewModel -

    public class TestViewModel
    {
        public List<InnerViewModel> ModelData { get; set; }
        public class InnerViewModel
        {
            public int Id { get; set; }
            public bool Checked { get; set; }
        }
    }
    

    控制器 -

        public ActionResult Index()
        {
            var test = new TestViewModel();
    
            test.ModelData = new List<TestViewModel.InnerViewModel>()
            {
                new TestViewModel.InnerViewModel {Id = 10},
                new TestViewModel.InnerViewModel {Id = 20},
                new TestViewModel.InnerViewModel {Id = 30},
                new TestViewModel.InnerViewModel {Id = 40}
            };
    
            return View(test);
        }
    
        public string TestAction(TestViewModel model)
        {
            string s = "";
            foreach (TestViewModel.InnerViewModel innerViewModel in model.ModelData)
            {
                if (innerViewModel.Checked)
                    s += innerViewModel.Id + " ";
            }
            return s;
        }
    

    查看 -

    @model MVC.Controllers.TestViewModel
    
    @using (Html.BeginForm("TestAction", "Home"))
    {
        <ol>
            @for (int i = 0; i < Model.ModelData.Count() ; i++ )
            {
                <li>
                    @Html.HiddenFor(m => m.ModelData[i].Id)
                    @Html.CheckBoxFor(m => m.ModelData[i].Checked)
                </li>
            }
        </ol>
    
        <input type="submit" />
    }
    

    【讨论】:

      【解决方案2】:

      您需要了解模型绑定器的工作原理。明白了就简单了。

      MVC Binding to checkbox

      【讨论】:

        【解决方案3】:

        复杂对象需要索引才能让模型绑定器拾取它们。

        将其更改为这样,以便模型绑定器将它们拾取:

            @for (int i = 0; i < Model.ModelData.Count; i++)
            {
                <li>
                    @Html.HiddenFor(m => Model.ModelData[i].Id)
                    @Html.CheckBoxFor(m => Model.ModelData[i].Checked)
                </li>
            }
        

        这是一篇很好的文章,解释了模型绑定中的一些问题。

        http://msdn.microsoft.com/en-us/magazine/hh781022.aspx

        【讨论】:

        • 将 foreach 更改为 for 并没有真正对事物的宏伟计划产生任何影响,更不用说在解决这个问题方面真正产生影响......他们最终都做了同样的事情.
        • @IyaTaisho 这是不正确的。在这种情况下,您必须使用 for 而不是 foreach 模型绑定器才能正常工作。
        • @digitalid 谢谢同意,我也被否决了。难以置信的。 :)
        • @lyataisho 是的,您确实需要一个 for,您发送的链接是针对行为不同的编辑器的。阅读我的答案文章中的链接以了解它。
        • @hutchonoid 这确实有效。我接受了 ramiramilu 的回答,因为他是第一个,但基本上是一样的,所以我可以确认您的解决方案有效。谢谢。
        【解决方案4】:

        一种想法是使用 CheckBoxFor 控件。它最终为您节省了很多麻烦来查找已检查的内容和未检查的内容。我之前构建了一个 RadioButtonListFor ,这并不是很困难。其实这里有一个链接可以用在上面。

        Create MVC3 CheckBoxFor from List and getting the list back (With updated values) on Post

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-09-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-07-05
          • 2014-03-09
          • 1970-01-01
          相关资源
          最近更新 更多