C# asp.net mvc中的编辑视图
我有表callC# asp.net mvc中的编辑视图,c#,asp.net-mvc,linq,C#,Asp.net Mvc,Linq,我有表callAB\u Product\u vs\u Field在该表中我有以下列 产品标识 字段ID 字段值 一旦我通过Product_ID和Field_ID,我想在该表中找到相关记录并加载相关的Field_值 为此,我编写了以下代码 [HttpGet] public ActionResult Edit(string Product_ID , string Field_ID) { if ((db.AB_Product_vs_Field.Any(u =
AB\u Product\u vs\u Field
在该表中我有以下列
- 产品标识
- 字段ID
- 字段值
Product_ID
和Field_ID
,我想在该表中找到相关记录并加载相关的Field_值
为此,我编写了以下代码
[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
model类
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; }
}
这是该编辑视图的视图
@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>
}
@model albaraka.Models.ProductEdit
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@for(int i=0;ix.ListProductLables[i].ProductFieldNameEn,Model.ListProductLables[i].ProductFieldNameEn,htmlAttributes:new{@class=“control label col-md-2”})
@Html.HiddenFor(m=>m.ListProductFields[i].Field\u ID)
@Html.TextAreaFor(m=>m.ListProductFields[i].Field_值,新的{@class=“form control”,@row=5})
}
}
然后我得到了流动误差
索引超出范围。必须为非负数且小于
收藏。参数名称:索引
我的方法有什么问题,如何正确加载 您需要将“&”运算符更改为“&&”运算符:
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" })
致:
最可能的原因:
@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(int i=0;ix.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
的元素数不完全相同,您将得到异常查看整个错误。它将指定导致此错误的行的位置以及源文件的内容。它的此行@Html.LabelFor(x=>x.ListProductLables[i].ProductFieldNameEn,Model.ListProductLables[i].ProductFieldNameEn,htmlAttributes:new{@class=“control label col-md-2”}
在控制器中像var abProduct=db.AB\u Product\u vs\u Field那样填充它。其中(p=>p.Field\u ID==FieldID)。ToList();ListProductFields=从abProduct中的一个字段选择新的AB\u Product\u vs\u Field{Product_ID=abProduct.Product_ID,Field_ID=abProduct.Field_ID,Field_Value=abProduct.Field_Value}我认为这个linq查询产生了问题ListProductFields=db.AB\u Product\u vs\u Field.Where(p=>p.Field\u ID==FieldID).ToList(),
我想选择特定的行,但它似乎抓取了多行,是吗?如果只需要一行,那么使用.Where(p=>p.Field\u ID==FieldID).FirstOrDefault()
并将您的属性公开为AB_Product\u vs_Field ListProductFields{get;set;}
(即不是集合)当然,你可以删除视图中的循环。我想用两个参数找到一个特定的行,所以我试着这样做ListProductFields=db.AB\u Product\u vs\u Field.Where((db.AB\u Product\u vs\u Field.Any(u=>u.Product\u ID==Product\u ID))&(db.AB\u Product\u vs\u Field.Any(u=>u.Field\u ID==Field\u ID))).ToList(),
但这有一个复杂的时间错误啊,好吧,按如下方式排序:ListProductFields=db.AB\u Product\u vs\u Field.Where((p=>p.Field\u ID==FieldID)).Where(p=>p.Product\u ID==Product\u ID.ToList(),
很抱歉,您仍然在使用.ToList()
您希望返回一个集合还是只返回一个对象?
@Html.LabelFor(x => x.ListProductLables[i].ProductFieldNameEn....
@Html.LabelFor(x => x.ListProductFields[i].someProperty