1

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 ?

3
  • 1
    See your entire error. It will specify you where is the line that causes this error and what is the source file. Commented Jan 14, 2016 at 7:07
  • 1
    its this line @Html.LabelFor(x => x.ListProductLables[i].ProductFieldNameEn, Model.ListProductLables[i].ProductFieldNameEn, htmlAttributes: new { @class = "control-label col-md-2" }) Commented Jan 14, 2016 at 7:10
  • 1
    IN controller fill it like var abProduct =db.AB_Product_vs_Field.Where(p => p.Field_ID == FieldID).ToList(); ListProductFields = from a in abProduct select new AB_Product_vs_Field { Product_ID = abProduct.Product_ID , Field_ID = abProduct.Field_ID , Field_Value = abProduct.Field_Value } Commented Jan 14, 2016 at 7:29

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

13 Comments

I think this linq query generate the problem ListProductFields = db.AB_Product_vs_Field.Where(p => p.Field_ID == FieldID).ToList(), I want to select specific row , but seems like its grabbing multiple rows , is it ?
If you only want one row, then use .Where(p => p.Field_ID == FieldID).FirstOrDefault(); and make your property public AB_Product_vs_Field ListProductFields { get; set; } (i.e. not a collection). And ditto for ListProductLables. And of course you can then remove the loop in the view
I want to find a specific row using 2 parameters So I tried like this ListProductFields = db.AB_Product_vs_Field.Where((db.AB_Product_vs_Field.Any(u => u.Product_ID == Product_ID)) & (db.AB_Product_vs_Field.Any(u => u.Field_ID == Field_ID))).ToList(), but this has a comiple time error
ahh okay sorted it like this ListProductFields = db.AB_Product_vs_Field.Where((p => p.Field_ID == FieldID)).Where(p =>p.Product_ID == Product_ID).ToList(), sorry for the bother
Your still using .ToList() Are you expecting to return a collection or just a single object?
|
1

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.

Comments

0

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" }) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.