CheckboxFor不是嵌套对象绑定 [英] CheckboxFor not binding with nested objects

查看:137
本文介绍了CheckboxFor不是嵌套对象绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当一个属性嵌套在模型中的对象定义CheckBoxFor是无界?

下面是一个例子。我有一个包含列表℃的 SearchOptions 模式;明星> 属性。每个都有一个编号,名称和应限定的布尔属性:

 公共类SearchOptions
{    公共SearchOptions()
    {
        // 默认值
        星星=新的List<星GT&()
        {
            新之星(){总数= 1,名称= Resources.Home.Index.Star1,
                IsSelected =虚假},
            新之星(){总数= 2,名称= Resources.Home.Index.Star2,
                IsSelected =虚假},
            新之星(){总数= 3,名称= Resources.Home.Index.Star3,
                IsSelected = TRUE},
            新之星(){总数= 4,名称= Resources.Home.Index.Star4,
                IsSelected = TRUE},
            新之星(){数= 5,名称= Resources.Home.Index.Star5,
                IsSelected = TRUE},
        };
    }    公开名单<星GT&;星光{搞定;组; }}

在我的强类型的视图(的 SearchOptions )我遍历属性:

  @using(Html.BeginForm(做,家))
{
    <&字段集GT;
        <传奇> @ MVC3TestApplication.Resources.Home.Index.Search< /传说>
        @ {
            的foreach(星S IN Model.Stars)
           {
                @ Html.CheckBoxFor(M = GT; s.IsSelected)
                <标签> @ s.Name< /标签>           }}
    < /字段集>
    <输入类型=提交值=Invia/>
}

控制器(相关部分)是:

 公众的ActionResult SearchOptions()
    {
        返回查看(新SearchOptions());
    }    [HttpPost]
    公众的ActionResult DO(SearchOptions S)
    {
        //做一些东西
        返回视图(SearchOptions,S);
    }


解决方案

这是因为你是如何访问在 CheckBoxFor 前pression属性。

  @for(INT I = 0; I< Model.Stars.Count();我++){
    @ Html.CheckBoxFor(M = GT; m.Stars [I] .IsSelected)
    <标签> @ Model.Stars [I] .Name点< /标签>
}

这应该为你工作。

下面是来自不同方法的输出:

  //使用循环
<输入ID =Stars_2__IsSelectedNAME =星星[2] .IsSelected类型=复选框VALUE =真/>使用foreach //
<输入检查=选中ID =s_IsSelectedNAME =s.IsSelected类型=复选框VALUE =真/>

您会发现,为的foreach不包含适当的名称为它做匹配模型绑定时。

CheckBoxFor is not bounded when a property is defined in an object nested in the model?

Here is an example. I have a SearchOptions model that contains a List<Star> property. Each Star has a number, a name and a bool property that should be bounded:

public class SearchOptions
{

    public SearchOptions()
    {
        // Default values
        Stars = new List<Star>()
        {
            new Star() {Number=1, Name=Resources.Home.Index.Star1,
                IsSelected=false},
            new Star() {Number=2, Name=Resources.Home.Index.Star2,
                IsSelected=false},
            new Star() {Number=3, Name=Resources.Home.Index.Star3,
                IsSelected=true},
            new Star() {Number=4, Name=Resources.Home.Index.Star4,
                IsSelected=true},
            new Star() {Number=5, Name=Resources.Home.Index.Star5,
                IsSelected=true},
        };
    }

    public List<Star> Stars { get; set; }

}

In my strongly typed View (of SearchOptions) i loop over Stars property:

@using (Html.BeginForm("Do", "Home"))
{
    <fieldset>
        <legend>@MVC3TestApplication.Resources.Home.Index.Search</legend>
        @{ 
            foreach (Star s in Model.Stars)
           {
                @Html.CheckBoxFor(m => s.IsSelected)
                <label>@s.Name</label>

           }}
    </fieldset>
    <input type=submit value="Invia" />
}

The (relevant part of) controller is:

    public ActionResult SearchOptions()
    {
        return View(new SearchOptions());
    }

    [HttpPost]
    public ActionResult Do(SearchOptions s)
    {
        // Do some stuff
        return View("SearchOptions", s);
    }

解决方案

It's because of how you're accessing the properties in the CheckBoxFor expression.

@for (int i = 0; i < Model.Stars.Count(); i++) { 
    @Html.CheckBoxFor(m => m.Stars[i].IsSelected)
    <label>@Model.Stars[i].Name</label>
}

This should work for you.

Here's the output from the different methods:

//using the for loop
<input id="Stars_2__IsSelected" name="Stars[2].IsSelected" type="checkbox" value="true" />

//using the foreach
<input checked="checked" id="s_IsSelected" name="s.IsSelected" type="checkbox" value="true" />

You'll notice that the for foreach doesn't contain the proper name for it to match to when doing model binding.

这篇关于CheckboxFor不是嵌套对象绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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