0

CustomCssFields is null when going back to controller. I already set a constructor but it is still the same. I found some similar question MVC Model with a list of objects as property but it almost has the same code as mine.

MODEL

public class CompanyModelView
{
    public CompanyModelView()
    {
        CustomCssFields = new List<CompanyCssReferenceModelView>();
    }
        [BsonId]
        public string _id { get; set; }

        [BsonElement("CompanyName")]
        [DisplayName("Company Name")]
        public string CompanyName { get; set; }

        [BsonElement("CompanyCode")]
        [DisplayName("Company Code")]
        public string CompanyCode { get; set; }

        [BsonElement("CompanyConnectionString")]
        [DisplayName("Company Connection String")]
        public string CompanyConnectionString { get; set; }

        [BsonElement("CargowiseVersion")]
        [DisplayName("Cargowise Version")]
        public string CargoWiseVersion { get; set; }

        [BsonElement("CustomCssFields")]
        [UIHint("CustomCssFields")]
        public IList<CompanyCssReferenceModelView> CustomCssFields { get; set; }
}

public class CompanyCssReferenceModelView
{
        [BsonElement("FieldName")]
        public CssOptionEnum FieldName;

        [BsonElement("FieldValue")]
        public string FieldValue;
}

VIEW here is the view.

@model WebTrackerModels.CompanyModels.CompanyModelView
    <form asp-action="SaveCompany" enctype="multipart/form-data">
    <div class="form-group">
    <table class="table">
                        <thead>
                            <tr>
                                <th>
                                    Field Name
                                </th>
                                <th>
                                    Field Value
                                </th>
                            </tr>
                        </thead>
        <tbody>
         @{                            
              for (int i = 0; i < Model.CustomCssFields.Count; i++)
              {
                  <tr>
                      <td>                                        
                          @Html.DisplayFor(model => model.CustomCssFields[i].FieldName)
                      </td>
                      <td>
                          @Html.TextBoxFor(model => model.CustomCssFields[i].FieldValue, new { @class = "form-control", type = "color", value = Model.CustomCssFields[i].FieldValue })
                      </td>
                   </tr>
        }
        }
        </tbody>
    </table>
    </div>
          <div class="form-group">
               <input type="submit" value="Save" class="btn btn-success" />
           </div>
    </form>
Reynan
  • 163
  • 1
  • 1
  • 11

0 Answers0