将对象列表绑定到表单

编程入门 行业动态 更新时间:2024-10-25 01:30:08
本文介绍了将对象列表绑定到表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一类 Signature 对象:

公共类签名{公共 int SignatureID { 获取;放;}公共 int FormID { 获取;放;}公共字符串标题{获取;放;}公共字符串电子邮件{获取;放;}[显示(姓名=签署日期:")]公共日期时间?日期{得到;放;}}

我有一个 Form.cs 类,它有一个虚拟签名列表

公共虚拟列表签名{得到;放;}

在我的控制器中,我通过以下方式填充列表:

form.Signatures = repository.Signatures.Where(s => s.FormID == form.FormID).ToList();

在我的表单视图中,我显示了相关签名的列表:

@foreach(Model.Signatures 中的 var 签名){<div class="text-center"><label asp-for="@signature.Title"></label><input asp-for="@signature.Title"/><label asp-for="@signature.Email"></label><input asp-for="@signature.Email"/><label asp-for="@signature.Date"></label><input disabled asp-for="@signature.Date">

}

但是,我不知道如何在表单的 POST 方法上更新关联的签名.例如,如果我更改签名的 Email 属性并 POST 表单,模型不会将此更改绑定到 Form 对象.在这种情况下,form.Signatures 为空.

如何确保在 POST 时更新与表单关联的 Signature 项目的更改?

解决方案

使用 for 循环来生成元素,因为它会向模型绑定器使用的属性名称添加索引来绑定列出不适用于 foreach 的帖子:

@for (int i=0; i}

现在元素将使用 Signatures[0].Title、Signatures[1].Title 之类的名称呈现,并且模型绑定器可以将其绑定到模型上发布.

希望有帮助.

I have a class of Signature objects:

public class Signature { public int SignatureID { get; set; } public int FormID { get; set; } public string Title { get; set; } public string Email { get; set; } [Display(Name = "Signed Date:")] public DateTime? Date { get; set; } }

I have a Form.cs class that has a virtual list of signatures

public virtual List<Signature> Signatures { get; set; }

In my controller, I populate the list by:

form.Signatures = repository.Signatures.Where(s => s.FormID == form.FormID).ToList();

In my Form View, I display a list of the associated signatures:

@foreach (var signature in Model.Signatures) { <div class="text-center"> <label asp-for="@signature.Title"></label> <input asp-for="@signature.Title" /> <label asp-for="@signature.Email"></label> <input asp-for="@signature.Email" /> <label asp-for="@signature.Date"></label> <input disabled asp-for="@signature.Date"> </div> }

However, I don't know how to update the associated signatures upon my POST method of the form. For example, if I change the Email property of a signature and POST the form, the model does not bind this change into the Form object. In this case, form.Signatures is null.

How can I ensure changes to the <List>Signature items associated with the form are updated on POST?

解决方案

Use the for loop to generate the elements, as it would add indexing to the property names which is used by model binder to bind to List on the post which does not work with the foreach:

@for (int i=0; i< Model.Signatures.Count; i++) { <div class="text-center"> <label asp-for="@Model.Signatures[i].Title"></label> <input asp-for="@Model.Signatures[i].Title" /> <label asp-for="@Model.Signatures[i].Email"></label> <input asp-for="@Model.Signatures[i].Email" /> <label asp-for="@Model.Signatures[i].Date"></label> <input disabled asp-for="@Model.Signatures[i].Date"> </div> }

Now the elements would be rendered with names like Signatures[0].Title, Signatures[1].Title and the model binder can bind it to the model on post.

Hope it helps.

更多推荐

将对象列表绑定到表单

本文发布于:2023-10-07 17:30:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1470032.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   绑定   对象   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!