[英]Edit view in asp.net mvc
我在該表中有表調用AB_Product_vs_Field
,我有以下幾列
傳遞Product_ID
和Field_ID
我想在該表中找到相關記錄並加載相關的Field_Value
。
因此,我編寫了以下代碼
[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);
}
}
這是ProductEdit
模型類
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; }
}
這些是相關的模型類
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; }
}
這是該編輯視圖的視圖
@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>
}
那我就錯了
索引超出范圍。 必須為非負數並且小於集合的大小。 參數名稱:索引
我的方法有什么問題,如何正確加載?
最可能的原因:
@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" })
在這里,您假設在for
循環條件下ListProductFields.Count
將等於ProductFieldNameEn.Count
=
,但它們似乎不等於count。
您循環正在遞增ListProductFields
集合,但是在內部
@Html.LabelFor(x => x.ListProductLables[i].ProductFieldNameEn....
我認為應該
@Html.LabelFor(x => x.ListProductFields[i].someProperty
如果ListProductLables
不含有相同數量的元素作為ListProductLables
,您將獲得異常
您需要將'&'運算符更改為'&&'運算符:
if ((db.AB_Product_vs_Field.Any(u => u.Product_ID == Product_ID))&&(db.AB_Product_vs_Field.Any(u => u.Field_ID == FieldID)))
同樣在視圖中,您引用了錯誤的數組:
變化:
@Html.LabelFor(x => x.ListProductLables[i].ProductFieldNameEn, Model.ListProductLables[i].ProductFieldNameEn, htmlAttributes: new { @class = "control-label col-md-2" })
至 :
@Html.LabelFor(x => x.ListProductFields[i].ProductFieldNameEn, Model.ListProductFields[i].ProductFieldNameEn, htmlAttributes: new { @class = "control-label col-md-2" })
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.