I have table call AB_Product_vs_Field in that table I have following columns
- Product_ID
- Field_ID
- Field_Value
once I pass Product_ID and Field_ID I want to find the relavant record in that table and load the relevant Field_Value.
So for that I wrote following code
[HttpGet] public ActionResult Edit(string Product_ID , string Field_ID) { if ((db.AB_Product_vs_Field.Any(u => u.Product_ID == Product_ID))&(db.AB_Product_vs_Field.Any(u => u.Field_ID == FieldID))) { var product_values = new ProductEdit { ListProductFields = db.AB_Product_vs_Field.Where(p => p.Field_ID == FieldID).ToList(), ListProductLables = db.AB_ProductTypeCategoryField.Where(p => p.ProductFieldID == FieldID).ToList(), Pager = pager }; return View(product_values); } } this is ProductEdit model class
public class ProductEdit { public string Product_ID { get; set; } public string Field_ID { get; set; } public IList<AB_Product_vs_Field> ListProductFields { get; set; } public IList<AB_ProductTypeCategoryField> ListProductLables { get; set; } public IEnumerable<string> Items { get; set; } public PaginationModel.Pager Pager { get; set; } public int PageSize { get; set; } public int PageCount { get; set; } public int CurrentPageIndex { get; set; } } these are the relevant model classes
public partial class AB_ProductTypeCategoryField { public string ProductFieldID { get; set; } public string ProductFieldNameEn { get; set; } } public partial class AB_Product_vs_Field { public string Product_ID { get; set; } public string Field_ID { get; set; } public string Field_Value { get; set; } } this is the view of that edit view
@model albaraka.Models.ProductEdit @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal"> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) @for (int i = 0; i < Model.ListProductFields.Count; i++) { <div class="form-group"> @Html.LabelFor(x => x.ListProductLables[i].ProductFieldNameEn, Model.ListProductLables[i].ProductFieldNameEn, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.HiddenFor(m => m.ListProductFields[i].Field_ID) @Html.TextAreaFor(m => m.ListProductFields[i].Field_Value, new { @class = "form-control", @row = 5 }) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Save Details" class="btn btn-success" /> </div> </div> } </div> } then I'm getting flowing error
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
whats wrong with my approach , how to load properly ?
@Html.LabelFor(x => x.ListProductLables[i].ProductFieldNameEn, Model.ListProductLables[i].ProductFieldNameEn, htmlAttributes: new { @class = "control-label col-md-2" })