从列表与LT创建MVC3 CheckBoxFor; T>并在帖子添加到列表后面(使用更新的值) [英] Create MVC3 CheckBoxFor from List<T> and getting the list back (With updated values) on Post

查看:76
本文介绍了从列表与LT创建MVC3 CheckBoxFor; T>并在帖子添加到列表后面(使用更新的值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的ViewModel列表我解析到View

I have a List in my ViewModel I parse to the View

List<BoolSetting> 

BoolSetting:

BoolSetting:

    public class BoolSetting
{
    public BoolSetting(string displayName, bool value)
    {
        DisplayName = displayName;
        Value = value;
    }
    public string DisplayName { get; set; }
    public bool Value { get; set; }
}

然后我要打印一个复选框,在我的列表中的所有项目,因此该列表是在视图模型视图的使用

I then want to print a checkbox for all the items in my list, so the list is in the ViewModel the view uses

@foreach(var boolSettingList in Model.BoolSettingList)
        {
            <div>
                @Html.CheckBox(boolSettingList.DisplayName, boolSettingList.Value)
                @boolSettingList.DisplayName
            </div>
        }

问题是,当我在视图模型发布这则我的模型尚未保存在列表中的更新设置(布尔值),因此对象是空的。

The problem is when I post this then my Model have not saved the updated settings (The bool values) in the List in my ViewModel and therefore the object is empty.

我可以做

foreach (var VARIABLE in userSettingConfigViewModel.BoolSettingList)
        {
            VARIABLE.Value = (bool)Request.Form[VARIABLE.DisplayName];

        }

但这个视图模型将有许多列表,其中一些将有相同的名字!这样会造成冲突

But this viewmodel will have many lists and some of them will have the same names! So that will cause conflicts

那么,有没有一种方法来打印的foreach我所有的布尔变量,然后让MVC找出把数据回列表对象之后?我不能让CheckBoxFor工作,因为它要一个前pression,我不能想出一个办法,通过我的列表,以itterate这样

So is there a way to foreach print all my bools and then make MVC figure out to put the data back into the List object after? I cant get CheckBoxFor to work as it wants an expression and I cant figure out a way to itterate through my list that way

我可以或许用模板修复它,通过对BoolSetting模板,也许列表?

Can I maybe fix it with Templates, by making a template for BoolSetting and maybe List ?

推荐答案

通过固定您的视图模型和删除自定义构造函数或默认的模型绑定将无法对其进行实例开始,你将不得不编写自定义模型粘合剂和东西:

Start by fixing your view model and removing the custom constructor or the default model binder won't be able to instantiate it and you will have to write custom model binders and stuff:

public class BoolSetting
{
    public string DisplayName { get; set; }
    public bool Value { get; set; }
}

public class MyViewModel
{
    public List<BoolSetting> Settings { get; set; }
}

然后写一个控制器的动作,将填充您的视图模型:

Then write a controller action that will populate your view model:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            Settings = new[] 
            {
                new BoolSetting { DisplayName = "name 1", Value = true },
                new BoolSetting { DisplayName = "name 2", Value = false },
                new BoolSetting { DisplayName = "name 3", Value = true },
            }.ToList()
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        return View(model);
    }
}

然后视图(〜/查看/主页/ Index.cshtml ),其中只需使用编辑模板,不写任何的foreach 循环或弱类型的HTML辅助,如 Html.CheckBox 。通过使用模板编辑器,你将确保所有输入字段都会有正确的名称,以便默认模型绑定能够回发时它们的值取到视图模型:

then a view (~/Views/Home/Index.cshtml) in which you simply use editor templates and don't write any foreach loops or weakly typed html helpers such as Html.CheckBox. By using editor templates you will ensure that all input fields will have correct names so that the default model binder is able to fetch their values into the view model during the postback:

@model MyViewModel
@using (Html.BeginForm())
{
    @Html.EditorFor(x => x.Settings)
    <button type="submit">OK</button>
}

终于为将被渲染为集合中的每个元素的视图模型对应的编辑器模板(〜/查看/主页/ EditorTemplates / BoolSetting.cshtml ):

@model BoolSetting
<div>
    @Html.CheckBoxFor(x => x.Value)
    @Html.LabelFor(x => x.Value, Model.DisplayName)
    @Html.HiddenFor(x => x.DisplayName)
</div>

这篇关于从列表与LT创建MVC3 CheckBoxFor; T&GT;并在帖子添加到列表后面(使用更新的值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆