وقت بخیر. من ی جدول با نام Product-Color دارم که رابط بین دو جدول Color و Product. تو این جدول علاوه بر Id های جداول Product و Color، یک فیلد با نام Quantity هم دارم. می خوام این مقادیر رو هنگام ویرایش تو view نشون بدم که ب خطای"ArgumentOutOfRangeException: Index was out of range.Must be non-negative and less than the size of the collection.(Parameter 'index')
"برخورد می کنم.
جدول دیتا:
Query که توی سرویس زدم:
public Tuple<List<Color>, List<GetColorQuantitiesForShow>> GetProductColors(int productId)
{
List<Color> colors = _context.Colors.ToList();
List<GetColorQuantitiesForShow> quantities = _context.ProductColors
.Where(w => w.ProductId == productId)
.Select(s =>
new GetColorQuantitiesForShow()
{
ColorId = s.ColorId,
Quantity = s.Quantity
}).ToList();
return Tuple.Create(colors, quantities);
}
ارسال از طریق ViewData به view:
ViewData["ColorQuantities"] = _productService.GetProductColors(id);
نمایش در view:
@{
var ColorQuantity = ViewData["ColorQuantities"] as Tuple<List<Color>, List<GetColorQuantitiesForShow>>;
}
@for (int i = 0; i < ColorQuantity.Item1.Count; i++)
{
<div>
<input type="checkbox" name="SelectedColors" value="@ColorQuantity.Item1[i].ColorId" @((ColorQuantity.Item1.Any(a => a.ColorId == ColorQuantity.Item1[i].ColorId) ? "checked" : "")) /> @ColorQuantity.Item1[i].ColorName
@if (ColorQuantity.Item2[i].Quantity is null)
{
<div>
<label>تعداد</label>
<input type="number" name="ColorQuantities" value="0" class="form-control" />
<p class="help-block">
در صورت عدم انتخاب این رنگ، تعداد آن را "صفر" قرار دهید !
</p>
</div>
}
else
{
<div>
<label>تعداد</label>
<input type="number" name="ColorQuantities" value="@ColorQuantity.Item2[i].Quantity" class="form-control" />
<p class="help-block">
در صورت عدم انتخاب این رنگ، تعداد آن را "صفر" قرار دهید !
</p>
</div>
}
<hr />
</div>
}
</div>