简体   繁体   English

在ASP.NET MVC中编辑视图

[英]Edit view in asp.net mvc

I have table call AB_Product_vs_Field in that table I have following columns 我在该表中有表调用AB_Product_vs_Field ,我有以下几列

  • Product_ID PRODUCT_ID
  • Field_ID FIELD_ID
  • Field_Value FIELD_VALUE

once I pass Product_ID and Field_ID I want to find the relavant record in that table and load the relevant Field_Value . 传递Product_IDField_ID我想在该表中找到相关记录并加载相关的Field_Value

So for that I wrote following code 因此,我编写了以下代码

    [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);           

        }

    }

this is ProductEdit model class 这是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; }

    }

these are the relevant model 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; }               
}

this is the view of that edit view 这是该编辑视图的视图

@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>
}

then I'm getting flowing error 那我就错了

Index was out of range. 索引超出范围。 Must be non-negative and less than the size of the collection. 必须为非负数并且小于集合的大小。 Parameter name: index 参数名称:索引

whats wrong with my approach , how to load properly ? 我的方法有什么问题,如何正确加载?

Most potential reason: 最可能的原因:

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

Here you are assuming in for loop condition that ListProductFields.Count will be = to ProductFieldNameEn.Count but they seem to be not equal count. 在这里,您假设在for循环条件下ListProductFields.Count将等于ProductFieldNameEn.Count = ,但它们似乎不等于count。

You loop is incrementing the ListProductFields collection, but inside you have 您循环正在递增ListProductFields集合,但是在内部

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

I assume it should be 我认为应该

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

If ListProductLables does not contain exactly the same number of elements as ListProductLables , your will get the exception 如果ListProductLables不含有相同数量的元素作为ListProductLables ,您将获得异常

You need to change the '&' operator to an '&&' operator : 您需要将'&'运算符更改为'&&'运算符:

  if ((db.AB_Product_vs_Field.Any(u => u.Product_ID == Product_ID))&&(db.AB_Product_vs_Field.Any(u => u.Field_ID == FieldID)))

Also in the view you are referencing the wrong array: 同样在视图中,您引用了错误的数组:

change : 变化:

   @Html.LabelFor(x => x.ListProductLables[i].ProductFieldNameEn, Model.ListProductLables[i].ProductFieldNameEn, htmlAttributes: new { @class = "control-label col-md-2" })

to : 至 :

   @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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM