coo*_*and 4 asp.net-mvc model-binding asp.net-core
我有一类Signature对象:
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; }
}
Run Code Online (Sandbox Code Playgroud)
我有一Form.cs堂课有一个虚拟的签名列表
public virtual List<Signature> Signatures { get; set; }
Run Code Online (Sandbox Code Playgroud)
在控制器中,通过以下方式填充列表:
form.Signatures = repository.Signatures.Where(s => s.FormID == form.FormID).ToList();
Run Code Online (Sandbox Code Playgroud)
在表单视图中,显示相关签名的列表:
@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>
}
Run Code Online (Sandbox Code Playgroud)
不过,我不知道如何在表格的我POST方法更新相关联的签名。例如,如果我改变了Email一个签名和POST形式的财产,该模型不与这种变化成Form对象。在这种情况下,form.Signatures为空。
如何确保<List>Signature在POST上更新与表单关联的项目的更改?
使用for循环来生成元素,因为它会增加索引到用于通过模型绑定绑定到列表上不与工作后的属性名称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>
}
Run Code Online (Sandbox Code Playgroud)
现在的元素将通过类似的名字被渲染Signatures[0].Title,Signatures[1].Title并且模型绑定可以将其绑定到后期的模型。
希望能帮助到你。
| 归档时间: |
|
| 查看次数: |
4216 次 |
| 最近记录: |