asp.net mvc2.0 model 中包含泛型的字段的处理

在asp.net mvc2.0中做开发,model 中包含泛型的字段字段的情况很常见,但网上一直很难找到相关的例子,没办法,只能自己摸索。分享如下。

代码:

 <table class="checkBoxList">
                                            <%  for (int i = 0; i < Model.CompanyBasicIndustry.Count; i++)
                                                {%>
                                            <%   if (i % 3 == 0)
                                                 {%>
                                            <tr>
                                                <%   }%>
                                                <td>
                                                    <span>
                                                        <%: Html.CheckBoxFor(m => Model.CompanyBasicIndustry[i].CheckState, new { @class = "checkboxStl" })%></span>
                                                    <span>
                                                        <%:Html.DisplayFor(m => Model.CompanyBasicIndustry[i].Text)%>
                                                        <%:Html.HiddenFor(m => Model.CompanyBasicIndustry[i].Text)%>
                                                        <%:Html.HiddenFor(m => Model.CompanyBasicIndustry[i].Value)%>
                                                    </span>
                                                </td>
                                                <%   if (i % 3 == 2 || i == Model.CompanyBasicIndustry.Count - 1)
                                                     {%>
                                            </tr>
                                            <%   }%>
                                            <%   }%>
                                        </table>

 

注意点:

1一定要用for循环,不能用foreach,非常奇怪,否则提交时,泛型字段的改变在后台不能收到,即在前台的改变,提交后,后台没办法知道。

   具体原因,个人猜测是因为foreach产生的编辑ID相同,提交时没办法区分。

2在建立model是泛型字段也应该有set,get,否则后台无法获知前台的改变。

3对于DisplayFor等不提交的字段如果需要提交,就需要使用一个隐藏字段保存它。否则提交后这些字段就会变成默认值,字符串就为空了。

 C#代码下载

作者:xuexiaodong2009 发表于2011-5-4 11:28:00 原文链接
阅读:74 评论:0 查看评论

你可能感兴趣的:(asp.net)