【问题标题】:Edit view in asp.net mvc在 asp.net mvc 中编辑视图
【发布时间】:2016-01-14 07:06:03
【问题描述】:

我在该表中有表调用 AB_Product_vs_Field 我有以下列

  • Product_ID
  • Field_ID
  • 字段值

一旦我通过了Product_IDField_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>
}

然后我得到流动错误

索引超出范围。必须是非负数且小于 集合。参数名称:索引

我的方法有什么问题,如何正确加载?

【问题讨论】:

  • 查看您的整个错误。它将为您指定导致此错误的行在哪里以及源文件是什么。
  • 这一行@Html.LabelFor(x =&gt; x.ListProductLables[i].ProductFieldNameEn, Model.ListProductLables[i].ProductFieldNameEn, htmlAttributes: new { @class = "control-label col-md-2" })
  • IN 控制器像 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 }

标签: c# asp.net-mvc linq


【解决方案1】:

您的循环正在增加 ListProductFields 集合,但在您的内部有

@Html.LabelFor(x => x.ListProductLables[i].ProductFieldNameEn....

我认为应该是

@Html.LabelFor(x => x.ListProductFields[i].someProperty

如果 ListProductLables 不包含与 ListProductLables 完全相同数量的元素,您将获得异常

【讨论】:

  • 我认为这个 linq 查询会产生问题ListProductFields = db.AB_Product_vs_Field.Where(p =&gt; p.Field_ID == FieldID).ToList(), 我想选择特定的行,但似乎它抓取了多行,是吗?
  • 如果您只想要一行,请使用.Where(p =&gt; p.Field_ID == FieldID).FirstOrDefault(); 并将您的属性设为public AB_Product_vs_Field ListProductFields { get; set; }(即不是集合)。 ListProductLables 也是如此。当然你也可以移除视图中的循环
  • 我想使用 2 个参数找到一个特定的行所以我尝试了这样 ListProductFields = db.AB_Product_vs_Field.Where((db.AB_Product_vs_Field.Any(u =&gt; u.Product_ID == Product_ID)) &amp; (db.AB_Product_vs_Field.Any(u =&gt; u.Field_ID == Field_ID))).ToList(), 但这有一个comiple time error
  • 啊,好吧,这样排序ListProductFields = db.AB_Product_vs_Field.Where((p =&gt; p.Field_ID == FieldID)).Where(p =&gt;p.Product_ID == Product_ID).ToList(),抱歉打扰
  • 您仍在使用.ToList() 您希望返回一个集合还是仅返回一个对象?
【解决方案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,但它们的计数似乎不相等。

【讨论】:

    【解决方案3】:

    您需要将“&”运算符更改为“&&”运算符:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-12
      • 1970-01-01
      • 2010-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多