Reputation: 2313
I have table call AB_Product_vs_Field
in that table I have following columns
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 ?
Upvotes: 1
Views: 80
Reputation:
You loop is incrementing the ListProductFields
collection, but inside you have
@Html.LabelFor(x => x.ListProductLables[i].ProductFieldNameEn....
I assume it should be
@Html.LabelFor(x => x.ListProductFields[i].someProperty
If ListProductLables
does not contain exactly the same number of elements as ListProductLables
, your will get the exception
Upvotes: 1
Reputation:
Most potential reason:
@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" })
Here you are assuming in for
loop condition that ListProductFields.Count
will be =
to ProductFieldNameEn.Count
but they seem to be not equal count.
Upvotes: 1
Reputation: 1957
You need to change the '&' operator to an '&&' operator :
if ((db.AB_Product_vs_Field.Any(u => u.Product_ID == Product_ID))&&(db.AB_Product_vs_Field.Any(u => u.Field_ID == FieldID)))
Also in the view you are referencing the wrong array:
change :
@Html.LabelFor(x => x.ListProductLables[i].ProductFieldNameEn, Model.ListProductLables[i].ProductFieldNameEn, htmlAttributes: new { @class = "control-label col-md-2" })
to :
@Html.LabelFor(x => x.ListProductFields[i].ProductFieldNameEn, Model.ListProductFields[i].ProductFieldNameEn, htmlAttributes: new { @class = "control-label col-md-2" })
Upvotes: 0