我有以下get方法来添加表单数据和编辑表单数据
在该表中也有表格AB_Product_vs_Field
我有以下列
一旦我通过Product_ID
和Field_ID
页码,就可以找到AB_Product_vs_Field
否则它没有记录我可以将插入作为新记录加载
[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);
}
}
这些是与上述控制器相关的模型
AddNewProduct模型类
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; }
}
这是ProductEdit模型类
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; }
}
这是添加新产品的视图
@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{}
这是产品字段值的编辑视图
@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{}
此添加视图应该逐个添加记录,每个产品最多15条记录,这些值将保存到AB_Product_vs_Field
,以便第一条记录正常工作。但是一旦我点击下一个或第二个,我就会出现以下错误
指数超出范围。必须是非负数且小于 集合。参数名称:index
错误发生在此行
{p}在@ Html.HiddenFor(m =&gt; m.ListProductFields [0] .Field_ID)
Add_Field_Values
页中