I have following get method to add a form data and edit form data

Also have table call AB_Product_vs_Field in that table I have following columns

  • Product_ID
  • Field_ID
  • Field_Value_EN
  • Field_Value_AR

once I pass Product_ID and Field_ID page number it can find if there any record exist in AB_Product_vs_Field

  • if it has a specific record I can load relevant values
  • else it doesn't have a record I can load insert as a new record

    [HttpGet]
    public ActionResult Add_Field_Values(int? page, string Product_ID,string FieldID)
    {
        var pager = new PaginationModel.Pager(dummyItems.Count(), page);
    
        ViewBag.Product_ID = Product_ID;
        ViewBag.FieldID = FieldID;
    
        //check specific row exist or not in AB_Product_vs_Field table
        if ((db.AB_Product_vs_Field.Any(u => u.Product_ID == Product_ID)) & (db.AB_Product_vs_Field.Any(u => u.Field_ID == FieldID)))
        {
            //edit a product field values
            var product_values = new ProductEdit
            {
    
                ListProductFields = db.AB_Product_vs_Field.Where((p => p.Field_ID == FieldID)).Where(p => p.Product_ID == Product_ID).ToList(),
                ListProductLables = db.AB_ProductTypeCategoryField.Where(p => p.ProductFieldID == FieldID).ToList(),
                Pager = pager,          
    
            };
    
            return View("~/Views/Home/Edit_Field_Values.cshtml", product_values);
    
        }
    
        else
        {
            //add new product field values
            var model = new AddNewProduct
            {
                ListProductFields_Add = db.AB_ProductTypeCategoryField.OrderBy(i => i.ProductFieldID).Skip((pager.CurrentPage - 1) * pager.PageSize).Take(pager.PageSize).ToList(),
                Pager = pager                   
            };
    
            return View(model);
    
        }
    }
    

theses are the the models that relate to above controller

AddNewProduct model class

public class AddNewProduct
{
    public string Product_ID { get; set; } }
    public string Field_ID { get; set; }
    public string ProductFieldNameEn { get; set; }
    public string ProductFieldNameAr { get; set; }
    public IList<AB_ProductTypeCategoryField> ListProductFields { 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; }
}

this is ProductEdit model class

public class ProductEdit
{
    public string Product_ID { get; set; }        
    public string Field_ID { get; set; }
    public string ProductFieldNameEn { get; set; }
    public string ProductFieldNameAr { 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; }
}

this is the view of Add New Product

@model project_name.Models.AddNewProduct


<h4>Add New Product</h4>


@using (Html.BeginForm("Add_Field_Values", "Home", new { Product_ID = ViewBag.Product_ID }, FormMethod.Post))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">


        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

       <div style="visibility:hidden"> @ViewBag.Product_ID </div>
       <div style="visibility:hidden"> @ViewBag.FieldID </div>


        @for (int i = 0; i < Model.ListProductFields.Count; i++)
        {
            <div class="form-group">
                @Html.LabelFor(x => x.ListProductFields[i].ProductFieldNameEn, Model.ListProductFields[i].ProductFieldNameEn, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.HiddenFor(m => m.ListProductFields[i].ProductFieldID
                    @Html.TextAreaFor(m => m.ListProductFields[i].Field_Value_EN, new { @class = "form-control summernote", @row = 5 })                    
                </div>
            </div>
            <div class="form-group">
                @Html.LabelFor(x => x.ListProductFields[i].ProductFieldNameAr, Model.ListProductFields[i].ProductFieldNameAr, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.HiddenFor(m => m.ListProductFields[i].ProductFieldID) 
                    @Html.TextAreaFor(m => m.ListProductFields[i].Field_Value_AR, new { @class = "form-control summernote", @row = 5 })
                </div>
            </div>

             <!-- pagination section -->

            <div class="form-group">
                <div class="col-md-10">
                    <!-- pager -->
                    @if (Model.Pager.EndPage > 1)
                    {
                        <ul class="pagination">
                            @if (Model.Pager.CurrentPage > 1)
                        {
                                <li>
                                    <a href="@Url.Action("Add_Field_Values","Home", new { Product_ID = ViewBag.Product_ID }, null)">First</a>
                                </li>
                                <li>
                                    <a href="@Url.Action("Add_Field_Values","Home", new { page = (Model.Pager.CurrentPage - 1), Product_ID = ViewBag.Product_ID }, null)">Previous</a>
                                </li>
                            }

                            @for (var page = Model.Pager.StartPage; page <= Model.Pager.EndPage; page++)
                        {
                                <li class="@(page == Model.Pager.CurrentPage ? "active" : "")">
                                    <a href="@Url.Action("Add_Field_Values","Home", new { page = page, Product_ID = ViewBag.Product_ID }, null)">@page</a>
                                </li>
                            }

                            @if (Model.Pager.CurrentPage < Model.Pager.TotalPages)
                        {
                                <li>
                                    <a href="@Url.Action("Add_Field_Values","Home", new { page = (Model.Pager.CurrentPage + 1), Product_ID = ViewBag.Product_ID }, null)">Next</a>
                                </li>
                                <li>

                                    <a href="@Url.Action("Add_Field_Values","Home", new { page = (Model.Pager.TotalPages), Product_ID = ViewBag.Product_ID }, null)">Last</a>
                                </li>
                            }
                        </ul>
                    }
                </div>
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Save Details" class="btn btn-success" />
                </div>

            </div>


        }

    </div>  }
     @section scripts{}

this is Edit view of product field values

@model project_name.Models.ProductEdit

<h4>Add New Product</h4>

@using (Html.BeginForm("Add_Field_Values", "Home", new { Product_ID = ViewBag.Product_ID }, FormMethod.Post))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">

        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div style="visibility:hidden"> @ViewBag.Product_ID </div>
        <div style="visibility:hidden"> @ViewBag.FieldID </div>

        <div class="form-group">
            @Html.LabelFor(x => x.ListProductLables[0].ProductFieldNameEn, Model.ListProductLables[0].ProductFieldNameEn, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.HiddenFor(m => m.ListProductFields[0].Field_ID)
                @if (Model.ListProductFields[0].Field_ID == "F000014" | Model.ListProductFields[0].Field_ID == "F000015")
                @Html.TextAreaFor(m => m.ListProductFields[0].Field_Value_EN, new { @class = "form-control summernote", @row = 5 })                
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(x => x.ListProductLables[0].ProductFieldNameAr, Model.ListProductLables[0].ProductFieldNameAr, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.HiddenFor(m => m.ListProductFields[0].Field_ID)
                @Html.TextAreaFor(m => m.ListProductFields[0].Field_Value_AR, new { @class = "form-control summernote", @row = 5 })

            </div>
        </div>

        <!-- pagination section -->

        <div class="form-group">

            <div class="col-md-10">
                <!-- pager -->
                @if (Model.Pager.EndPage > 1)
                {
                    <ul class="pagination">
                        @if (Model.Pager.CurrentPage > 1)
                        {
                            <li>
                                <a href="@Url.Action("Add_Field_Values","Home", new { Product_ID = ViewBag.Product_ID }, null)">First</a>
                            </li>
                                <li>
                                    <a href="@Url.Action("Add_Field_Values","Home", new { page = (Model.Pager.CurrentPage - 1), Product_ID = ViewBag.Product_ID }, null)">Previous</a>
                                </li>
                        }

                        @for (var page = Model.Pager.StartPage; page <= Model.Pager.EndPage; page++)
                        {
                            <li class="@(page == Model.Pager.CurrentPage ? "active" : "")">
                                <a href="@Url.Action("Add_Field_Values","Home", new { page = page, Product_ID = ViewBag.Product_ID }, null)">@page</a>
                            </li>
                        }

                        @if (Model.Pager.CurrentPage < Model.Pager.TotalPages)
                        {
                            <li>
                                <a href="@Url.Action("Add_Field_Values","Home", new { page = (Model.Pager.CurrentPage + 1), Product_ID = ViewBag.Product_ID }, null)">Next</a>
                            </li>
                                <li>

                                    <a href="@Url.Action("Add_Field_Values","Home", new { page = (Model.Pager.TotalPages), Product_ID = ViewBag.Product_ID }, null)">Last</a>
                                </li>
                        }
                    </ul>

                }

            </div>

            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save Details" class="btn btn-success" />
            </div>

        </div>


    </div>
    }
    @section scripts{}

This add view supposed to add record one by one upto 15 record per one product , those values are save to AB_Product_vs_Field for first record its working. but once I click next or number 2 I'm getting following error

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

error occur in this line

@Html.HiddenFor(m => m.ListProductFields[0].Field_ID)

in Add_Field_Values page

enter image description here

Related posts

Recent Viewed