MVC我如何创建一个包含列表作为属性模型的形式 [英] MVC How do I create a form for a model which contains lists as properties

查看:94
本文介绍了MVC我如何创建一个包含列表作为属性模型的形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个简单的模型:

 公共类选择禁用
    {
        公众诠释optOutID {搞定;组; }
        公共BOOL hasOptedOut {搞定;组; }
        公开名单<&队列GT;列表{搞定;组; }        公共选择禁用()
        {
            清单<&队列GT;名单=新名单,LT;队列>();
            list.Add(新队列());
            list.Add(新队列());
            list.Add(新队列());
            list.Add(新队列());
            this.list =清单;
        }    }

在哪里,你可以看到的属性之一是队列对象的列表。在队列对象有一些简单的布尔属性:

 公共类队列
    {
        公共BOOL ukft {搞定;组; }
        公共BOOL ukpt {搞定;组; }
        ...等等
    }

我想创建一个表单将通过队列的对象列表循环。

所以在我的控制器我通过模型到查看

 公众的ActionResult页()
        {
            返回查看(新选择禁用());
        }

然后在HTML创建一个这样的形式:

  @using(Html.BeginForm(OptedOut,家))
        {VAR M = Model.list;            < D​​IV ID =收音机级=表单组的风格=的margin-top:25px的;>
                @ Html.RadioButtonFor(型号=> Model.hasOptedOut,真)LT;跨度风格=保证金右:8像素;>是< / SPAN>
                @ Html.RadioButtonFor(型号=> Model.hasOptedOut,FALSE)<跨度&GT否LT; / SPAN> < BR />                //这里我通常会遍历列表中,但我改变了只用于测试目的的第一要素
                @ Html.RadioButtonFor(型号=> m.ElementAt(0).ukft,真)LT;跨度风格=保证金右:8像素;>是< / SPAN>
                @ Html.RadioButtonFor(型号=> m.ElementAt(0).ukft,FALSE)<跨度&GT否LT; / SPAN> < BR />
            < / DIV>            < D​​IV CLASS =表单组>
                <输入ID =确定类型=提交值=确认级=BTN BTN-成功BTN-LG/>
            < / DIV>
        }

到目前为止好。正如你所看到的,我已经添加了2套广播按钮 - 一个用于简单的 hasOptedOut 在布尔属性的选择禁用模型,一个是在第一个元素的 ukft 属性列表

现在,当我选择yes(真正)的两套单选按钮,我希望看到 hasOptedOut = TRUE 和第一 ukft = TRUE 队列对象的列表。所以我得到了控制模型后, POST

  [HttpPost]
公众的ActionResult OptedOut(Dlhe.Smart.Models.OptOut选择禁用)
{
    返回视图(some_view);
}

hasOptedOut 确实是真的。然而, ukft 保留在清单作虚伪。所以,我认为它不是那么简单了列表,但我怎么解决这个问题?

编辑:

的建议,我已经试过这样做,没有运气:

  @using(Html.BeginForm(OptedOut,家))
        {VAR M = Model.list;
            的for(int i = 0; I< Model.list.Count();我++){                < D​​IV ID =收音机级=表单组的风格=的margin-top:25px的;>                    @ Html.RadioButtonFor(型号=> M [​​] .ukft,真)LT;跨度风格=保证金右:8像素;>是< / SPAN>
                    @ Html.RadioButtonFor(型号=> M [​​] .ukft,FALSE)<跨度&GT否LT; / SPAN> < BR />
                < / DIV>
            }                < D​​IV CLASS =表单组的风格=的margin-top:25px的;>
                    <输入ID =确定类型=提交值=确认级=BTN BTN-成功BTN-LG/>
                < / DIV>
        }


解决方案

我觉得最简单的方法是为你的类型创建一个EditorTemplate。

要创建EditorTemplate:

在您的浏览

1)创建一个新文件夹/共享文件夹名为编辑模板

2)在该文件夹中创建一个新的,强类型的,局部的与类型的文件的名称的名称视图。例如Cohort.cshtml可能看起来像:

  @model Models.Cohort@ Html.RadioButtonFor(型号=> model.ukft,真)LT;跨度风格=保证金右:8像素;>是< / SPAN>
@ Html.RadioButtonFor(型号=> model.ukft,FALSE)<跨度&GT否LT; / SPAN> < BR />

3)在您的主视图中使用@ Html.EditorFor(型号=> model.list)和剃刀将呈现在步骤2中的列表会为每个队列的局部视图。

So I have this simple model:

public class OptOut
    {
        public int optOutID { get; set; }
        public bool hasOptedOut { get; set; }        
        public List<Cohort> list { get; set; }

        public OptOut()
        {
            List<Cohort> list = new List<Cohort>();
            list.Add(new Cohort());
            list.Add(new Cohort());
            list.Add(new Cohort());
            list.Add(new Cohort());
            this.list = list;
        }

    }

Where as you can see one of the properties is a List of Cohort objects. The Cohort object has some simple boolean properties:

public class Cohort
    {
        public bool ukft { get; set; }
        public bool ukpt { get; set; }
        ...etc
    }

I want to create a form which will loop through the list of Cohort objects.

So in my Controller I pass the model to the View:

public ActionResult Page()
        {                        
            return View(new OptOut());
        }

And then in the html I create a form like this:

 @using (Html.BeginForm("OptedOut", "Home"))
        {            var m = Model.list;

            <div id="radios" class="form-group" style="margin-top:25px;">
                @Html.RadioButtonFor(model => Model.hasOptedOut, true) <span style="margin-right:8px;">Yes</span>
                @Html.RadioButtonFor(model => Model.hasOptedOut, false) <span>No</span> <br />

                //here I would normally loop through the list but I'm changing only the first element for testing purposes
                @Html.RadioButtonFor(model => m.ElementAt(0).ukft, true) <span style="margin-right:8px;">Yes</span>
                @Html.RadioButtonFor(model => m.ElementAt(0).ukft, false) <span>No</span> <br />
            </div>

            <div class="form-group">
                <input id="confirm" type="submit" value="Confirm" class="btn btn-success btn-lg"/>
            </div>
        }

So far so good. As you can see, I've added 2 sets of radio buttons - one for the simple hasOptedOut boolean property in the OptOut model and one for the ukft property of the first element in the List.

Now when I select yes (true) in both sets of radio buttons, I should expect to see hasOptedOut = true and the first ukft = true for the Cohort object in the List. So I get the model in the controller after POST:

[HttpPost]
public ActionResult OptedOut(Dlhe.Smart.Models.OptOut optout)
{                
    return View("some_view");
}

And hasOptedOut is indeed true. However, ukft remains false in the List. So I assume it's not so simple for Lists, but how do I fix this?

EDIT:

As suggested, I have tried doing this, with no luck:

@using (Html.BeginForm("OptedOut", "Home"))
        {            var m = Model.list;
            for (int i=0; i<Model.list.Count(); i++) {

                <div id="radios" class="form-group" style="margin-top:25px;">

                    @Html.RadioButtonFor(model => m[i].ukft, true) <span style="margin-right:8px;">Yes</span>
                    @Html.RadioButtonFor(model => m[i].ukft, false) <span>No</span> <br />
                </div>                
            }

                <div class="form-group" style="margin-top:25px;">
                    <input id="confirm" type="submit" value="Confirm" class="btn btn-success btn-lg"/>
                </div>
        }

解决方案

The easiest way I think is to create an EditorTemplate for your type.

To create an EditorTemplate:

1) Create a new folder in your Views/Shared folder called Editor Templates

2) In that folder create a new, strongly typed, partial view with the name of your type as the name of the file. E.G Cohort.cshtml could look like:

@model Models.Cohort

@Html.RadioButtonFor(model => model.ukft, true) <span style="margin-right:8px;">Yes</span>
@Html.RadioButtonFor(model => model.ukft, false) <span>No</span> <br />

3) In your main view use @Html.EditorFor(model => model.list) and razor will render a partial view created in step 2 for each Cohort in the list.

这篇关于MVC我如何创建一个包含列表作为属性模型的形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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