在 asp.net mvc 中编辑视图
Edit view in asp.net mvc
我有 table 调用 AB_Product_vs_Field
table 我有以下列
- Product_ID
- Field_ID
- Field_Value
一旦我通过 Product_ID
和 Field_ID
我想在 table 中找到相关记录并加载相关 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
型号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; }
}
这些是相关模型 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; }
}
这是编辑视图的视图
@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>
}
然后我遇到了流动错误
Index was out of range. Must be non-negative and less than the size of
the collection. Parameter name: index
我的方法有什么问题,如何正确加载?
您需要将“&”运算符更改为“&&”运算符:
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" })
最可能的原因:
@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
但它们似乎不相等。
您的循环正在递增 ListProductFields
集合,但在您的内部有
@Html.LabelFor(x => x.ListProductLables[i].ProductFieldNameEn....
我想应该是
@Html.LabelFor(x => x.ListProductFields[i].someProperty
如果 ListProductLables
不包含与 ListProductLables
完全相同数量的元素,您将获得异常
我有 table 调用 AB_Product_vs_Field
table 我有以下列
- Product_ID
- Field_ID
- Field_Value
一旦我通过 Product_ID
和 Field_ID
我想在 table 中找到相关记录并加载相关 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
型号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; }
}
这些是相关模型 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; }
}
这是编辑视图的视图
@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>
}
然后我遇到了流动错误
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
我的方法有什么问题,如何正确加载?
您需要将“&”运算符更改为“&&”运算符:
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" })
最可能的原因:
@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
但它们似乎不相等。
您的循环正在递增 ListProductFields
集合,但在您的内部有
@Html.LabelFor(x => x.ListProductLables[i].ProductFieldNameEn....
我想应该是
@Html.LabelFor(x => x.ListProductFields[i].someProperty
如果 ListProductLables
不包含与 ListProductLables
完全相同数量的元素,您将获得异常