IEnumerable 的 mvc3 形式

发布于 2024-12-05 16:54:26 字数 1317 浏览 0 评论 0原文

我有一个模型,希望有一个批量更新页面,但遇到了麻烦。

我的 ViewModel:

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 />
}

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

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

I have a model that I would like have a mass update page for, but am having trouble.

My ViewModel:

public class ApproveView
{

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

}

In my view I have:

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 />
}

This loops over my objects and prints the form. The trouble is that all of the fields of the same type have the same name. So, for example, all of my radio buttons are connected and I can only select one.

how do I make the names for each field unique and associated with that object? Am I even on the right track or is there a better way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苹果你个爱泡泡 2024-12-12 16:54:27

检查这篇文章:http://haacked.com /archive/2008/10/23/model-binding-to-a-list.aspx

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

希望有帮助。

Check this post: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

You need to create editor for your entity.

Hope it helps.

幸福不弃 2024-12-12 16:54:27

您还可以手动执行更多操作:

    @{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++; 
@}

You could also do it a bit more manually:

    @{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++; 
@}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文