Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind List of objects to a form [duplicate]

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?

like image 824
coolhand Avatar asked Mar 19 '18 14:03

coolhand


1 Answers

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.

like image 135
Ehsan Sajjad Avatar answered Nov 14 '22 01:11

Ehsan Sajjad