vendredi 8 janvier 2016

MVC dynamic form controls binding to model

I'm trying to dynamically create a form with different types of fields. Then simply pass the user inputted data back to the controller and bind back to my model. I'm using a custom editor template for each control and was hoping it would bind properly in the controller. However, the property is NULL each time so I cannot retrieve the input values.

Model

public class ReceiptModel : ClassBase
{
    public int ReceiptId { get; set; }
    public List<CustomControlModel> CustomControlList { get; set; }
}

public class CustomControlModel 
{
    public string CustomControlName { get; set; }
    public CustomControlType CustomControlType { get; set; }
}

View

@foreach (CustomControlModel ccm in @Model.CustomControlList)
{
    if (!string.IsNullOrEmpty(ccm.PropertyName))
    {
        @Html.EditorFor(model => ccm, "CustomControlModel")
    }
}

Any help would be much appreciated. Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire