【问题标题】:mvc3 form for a IEnumerableIEnumerable 的 mvc3 表单
【发布时间】:2011-11-22 07:20:21
【问题描述】:

我有一个模型,我希望有一个大规模更新页面,但我遇到了问题。

我的视图模型:

public class ApproveView
{

    public IEnumerable<MyObject> ObjectList { get; set; }

}

在我看来,我有:

foreach (var item in Model.ObjectList)
{
    <div>


    <table class="form" width="100%">
        <tr>

            <td>@Html.LabelFor(model => item.Accurate)<br />
            @Html.RadioButtonFor(model => item.Accurate, true) Yes
            @Html.RadioButtonFor(model => item.Accurate, false) No
            @Html.ValidationMessageFor(model => item.Accurate)
            </td>
            <td>
            @Html.LabelFor(model => item.Comments)<br />
            @Html.TextAreaFor(model => item.Comments)<br />
            @Html.ValidationMessageFor(model => item.Comments)
            </td>
        </tr>

    </table>

    @Html.HiddenFor(model => item.ID)
    @Html.HiddenFor(model => item.CreatedOn)
    @Html.HiddenFor(model => item.CreatedBy)
    @Html.HiddenFor(model => item.ModifiedOn)
    @Html.HiddenFor(model => item.ModifiedBy)
   <hr />
}

这会遍历我的对象并打印表单。问题是所有相同类型的字段都具有相同的名称。因此,例如,我所有的单选按钮都已连接,我只能选择一个。

如何使每个字段的名称唯一并与该对象关联?我是否走在正确的轨道上,还是有更好的方法来做到这一点?

【问题讨论】:

    标签: asp.net-mvc-3 razor viewmodel


    【解决方案1】:

    查看此帖子:http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

    您需要为您的实体创建编辑器。

    希望对你有帮助。

    【讨论】:

      【解决方案2】:

      您也可以手动进行一些操作:

          @{var count = 0;}
          @foreach (var item in Model.ObjectList){
                  <div>
                      <table class="form">
                          <tr>
                              <td>@Html.Label("Accurate" + count, item.Accurate)<br />
                                  @Html.RadioButton("AccurateTrue" + count, true) Yes
                                  @Html.RadioButton("AccurateFalse" + count, false) No
                                  @Html.ValidationMessage("ValidationAccurate" + count, item.Accurate)
                              </td>
                              <td>
                                  @Html.Label("CommentsLabel" + count, item.Comments)<br />
                                  @Html.TextArea("Comments" + count, item.Comments)<br />
                                  @Html.ValidationMessage("ValidationComment" + count, item.Comments)
                              </td>
                          </tr>
                      </table>
                      @Html.Hidden("ID" + count, item.ID)
                      @Html.Hidden("CreatedOn" + count, item.CreatedOn)
                      @Html.Hidden("CreatedBy" + count, item.CreatedBy)
                      @Html.Hidden("ModifiedOn" + count, item.ModifiedOn)
                      @Html.Hidden("ModifiedBy" + count, item.ModifiedBy)
                      <hr />
                      @count++; 
      @}
      

      【讨论】:

      • 嗨。我对.net 很陌生。控制器中的保存代码如何寻找这样的东西?我让它显示,但我不知道如何将它保存到我的模型中。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-19
      • 2014-08-15
      • 1970-01-01
      • 2012-01-15
      • 2012-03-20
      • 2012-02-03
      相关资源
      最近更新 更多