ASP.NET MVC:用列表< model>绑定形式视图模型

发布于 2025-02-03 09:34:58 字数 1434 浏览 5 评论 0 原文

我有一个具有以下属性的视图模型:

// I set the values from the database
public List<Document> AvailableDocuments { get; set; } 

// I need to set the values from a front end <form>
public List<RequiredDocument> RequiredDocuments { get; set; } 

必需的document 模型包含以下属性:

// This should be an Id, maybe a hidden input
public Document Document { get; set; }

// This should be a number input
public int RequiredCopies { get; set; } 

// This should be a checkbox
public bool IsRequired { get; set; }

在我的视图中,我是通过 availabledocuments ,并且每个迭代都应绑定到必需的document 模型(用户可以在其中设置 suermcopies 编号的值)。

表格通过Ajax提交。如何将表格绑定到 surpeirdocuments

@foreach (Document doc in Model.AvailableDocuments)
{
    <div class="reqdoc">
        <!-- RequiredDocument.Document -->
        <input type="hidden" name="Document" value="@doc.Id" />
        <div class="form-check">
            <!-- RequiredDocument.IsRequired -->
            <input class="form-check-input" type="checkbox" value="" />
            <label class="form-check-label">
                @doc.Name
            </label>
        </div>

        <!-- RequiredDocument.RequiredCopies -->
        <input class="form-control" type="number" />
    </div>
}

I have a view model with the following properties:

// I set the values from the database
public List<Document> AvailableDocuments { get; set; } 

// I need to set the values from a front end <form>
public List<RequiredDocument> RequiredDocuments { get; set; } 

The RequiredDocument model contains the following properties:

// This should be an Id, maybe a hidden input
public Document Document { get; set; }

// This should be a number input
public int RequiredCopies { get; set; } 

// This should be a checkbox
public bool IsRequired { get; set; }

In my view I'm looping through AvailableDocuments and every iteration should bind to a RequiredDocument model (where the user may set the values for the RequiredCopies number).

The form is submitted via Ajax. How can I bind the form to RequiredDocuments?

@foreach (Document doc in Model.AvailableDocuments)
{
    <div class="reqdoc">
        <!-- RequiredDocument.Document -->
        <input type="hidden" name="Document" value="@doc.Id" />
        <div class="form-check">
            <!-- RequiredDocument.IsRequired -->
            <input class="form-check-input" type="checkbox" value="" />
            <label class="form-check-label">
                @doc.Name
            </label>
        </div>

        <!-- RequiredDocument.RequiredCopies -->
        <input class="form-control" type="number" />
    </div>
}

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

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

发布评论

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

评论(2

岁月苍老的讽刺 2025-02-10 09:34:58

您可以将这种循环使用我在项目中类似的循环;有用

<table>
@for (int i = 0; i < (int)ViewBag.Count; i++)
    {
        @Html.HiddenFor(model => model.AvailableDocuments.ToList()[i].ID)
        <tr>
            <td>
                @Html.CheckBoxFor(model => model.RequiredDocuments.ToList()[i].IsRequired, new { id = "chk_" + i, @class = "custom-checkbox" })
            </td>
            <td>
                @Html.DisplayFor(model => model.AvailableDocuments.ToList()[i].Name)
            </td>
            <td>
                @Html.TextBoxFor(model => model.RequiredDocument.ToList()[i].RequiredCopies, new { id = "RequiredCopies" + i, @class = "form-control" })
            </td>
        </tr>
    }
</table>

只需通过您的获取方法传递一个viewbag。

You can use this kind of for loop I am doing similar in my projects & it works

<table>
@for (int i = 0; i < (int)ViewBag.Count; i++)
    {
        @Html.HiddenFor(model => model.AvailableDocuments.ToList()[i].ID)
        <tr>
            <td>
                @Html.CheckBoxFor(model => model.RequiredDocuments.ToList()[i].IsRequired, new { id = "chk_" + i, @class = "custom-checkbox" })
            </td>
            <td>
                @Html.DisplayFor(model => model.AvailableDocuments.ToList()[i].Name)
            </td>
            <td>
                @Html.TextBoxFor(model => model.RequiredDocument.ToList()[i].RequiredCopies, new { id = "RequiredCopies" + i, @class = "form-control" })
            </td>
        </tr>
    }
</table>

Just pass a ViewBag.Count from Your get method.

如歌彻婉言 2025-02-10 09:34:58

使用索引并修改 name 属性为:

@{
    int i = 0;
    foreach (Document doc in Model.AvailableDocuments)
    {
    
        <div class="reqdoc">
            <!-- RequiredDocument.Document -->
            <input type="hidden" name="RequiredDocuments[@i].Document.Id" value="@doc.Id" />
            <div class="form-check">
                <!-- RequiredDocument.IsRequired -->
                <input class="form-check-input" type="checkbox" value="" name="RequiredDocuments[@i].IsRequired" />
                <label class="form-check-label">
                    @doc.Name
                </label>
            </div>

            <!-- RequiredDocument.RequiredCopies -->
            <input class="form-control" type="number" name="RequiredDocuments[@i].RequiredCopies" />
        </div>

        i++;
    }
}

Work with index and modify the name attribute as:

@{
    int i = 0;
    foreach (Document doc in Model.AvailableDocuments)
    {
    
        <div class="reqdoc">
            <!-- RequiredDocument.Document -->
            <input type="hidden" name="RequiredDocuments[@i].Document.Id" value="@doc.Id" />
            <div class="form-check">
                <!-- RequiredDocument.IsRequired -->
                <input class="form-check-input" type="checkbox" value="" name="RequiredDocuments[@i].IsRequired" />
                <label class="form-check-label">
                    @doc.Name
                </label>
            </div>

            <!-- RequiredDocument.RequiredCopies -->
            <input class="form-control" type="number" name="RequiredDocuments[@i].RequiredCopies" />
        </div>

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