我有一个我以前从未遇到的问题,所以这是一个基本的纲要。 我在用户可以提交保修索赔的网站上建立一个区域。在localhost上工作,因此无法提供链接。
我有一个用户输入姓名等的表格。 我有另一个用户输入产品信息的表格,现在这就是我的问题所在。用户可以为其保修索赔添加多个项目。 因此,下一步是添加另一项,或提交保修索赔。
现在我不想将数据添加到数据库,直到用户提交了所有信息,因此请点击提交保修声明按钮。
目前我有一个按钮来添加另一个项目,当用户点击添加另一个项目按钮时,我向服务器发送一个ajax请求。然后我清除产品信息表,然后用户可以添加第二个产品。
所以这是用户点击添加另一个项目按钮时触发的代码:
$.ajax({
cache: false,
url: '@(Url.RouteUrl("Nop.Plugin.Widgets.CustomerReturnsWidget.CustomerReturns.NoAccountWarrantyFormProduct"))',
type: 'post',
data: $("#add-warranty-claim-form-product-details").serialize(),
success: function (data) {
//reset form
$("#add-warranty-claim-form-product-details")[0].reset();
numItemsCount++;
}
});
这是最终提交按钮的ajax:
$.ajax({
cache: false,
url: '@(Url.RouteUrl("Nop.Plugin.Widgets.CustomerReturnsWidget.CustomerReturns.NoAccountWarrantyFormYourDetails"))',
type: 'post',
data: $("#add-warranty-claim-form, #add-warranty-claim-form-product-details").serialize(),
success: function (data) {
console.log("success = " + data.success + ":");
}
});
如您所见,我将两个表单序列化并发送给控制器。什么是最好的方法呢?
我想也许我可以有一个临时表,在那里输入该产品表格。然后,如果用户没有声明数据未输入保修索赔表。然后我可以构建一个每周运行一次的任务,并从那个超过一周的临时产品表中删除所有内容。但我觉得会有更好的解决方案。
现在还值得注意的是我在我的模型上使用验证,因此必须先对产品表单进行验证才能添加其他产品。
这是我的模特:
namespace Nop.Plugin.Widgets.CustomerReturnsWidget.Models
{
[Validator(typeof(CustomerReturnsValidator))]
public partial class CustomerReturnsModel : BaseNopModel
{
public CustomerReturnsModel() {
Items = new List<CustomerReturnItemModel>();
}
public int CustomerReturnID { get; set; }
//customer details
[NopResourceDisplayName("Plugin.CustomerReturns.Fields.CustomerEmail")]
public string CustomerEmail { get; set; }
[NopResourceDisplayName("Plugin.CustomerReturns.Fields.CustomerFullName")]
public string CustomerFullName { get; set; }
[NopResourceDisplayName("Plugin.CustomerReturns.Fields.CustomerPhoneNumber")]
public string CustomerPhoneNumber { get; set; }
[NopResourceDisplayName("Plugin.CustomerReturns.Fields.InvoiceNumber")]
public int? InvoiceNumber { get; set; }
[NopResourceDisplayName("Plugin.CustomerReturns.Fields.DateSubmitted")]
public DateTime? DateSubmitted { get; set; }
public IList<CustomerReturnItemModel> Items { get; set; }
[Validator(typeof(CustomerReturnItemValidator))]
public partial class CustomerReturnItemModel : BaseNopModel
{
public CustomerReturnItemModel()
{
ClaimOptionTypeList = new List<SelectListItem>();
}
public int Id { get; set; }
public int CustomerReturnID { get; set; }
//claim form warranty
[NopResourceDisplayName("Plugin.CustomerReturns.Fields.ClaimOptionType")]
public string ClaimOptionType { get; set; }
[NopResourceDisplayName("Plugin.CustomerReturns.Fields.PurchaseDate")]
public string PurchaseDate { get; set; }
[NopResourceDisplayName("Plugin.CustomerReturns.Fields.ProductBrand")]
public string ProductBrand { get; set; }
[NopResourceDisplayName("Plugin.CustomerReturns.Fields.ProductBrandQuantity")]
public int ProductBrandQuantity { get; set; }
[NopResourceDisplayName("Plugin.CustomerReturns.Fields.ProductName")]
public string ProductName { get; set; }
public IList<SelectListItem> ClaimOptionTypeList { get; set; }
}
}
}
以下是我的控制器的操作结果:
public ActionResult PartialNoAccountWarrantyProductFormView()
{
var model = new CustomerReturnsModel.CustomerReturnItemModel();
//type of claim form options
Dictionary<string, string> claimFormVals = new Dictionary<string, string>
{
{ "I'd like to submit a warranty claim", "0" },
{ "An item was damaged in transit", "1"},
{ "I'd like to return an item", "2" }
};
foreach (var val in claimFormVals)
{
model.ClaimOptionTypeList.Add(new SelectListItem
{
Text = val.Key,
Value = val.Value,
Selected = (model.ClaimOptionType != null && model.ClaimOptionType != "0") ? val.Value == model.ClaimOptionType : val.Value == "0"
});
}
return View("~/Plugins/Widgets.CustomerReturns/Views/CustomerReturns/PartialNoAccountWarrantyProductForm.cshtml", model);
}
对于添加另一个项目按钮,我发布到此控制器方法:
[HttpPost]
public ActionResult NoAccountWarrantyFormProduct(CustomerReturnsModel.CustomerReturnItemModel model, FormCollection form)
{
CustomerReturnsModel Model = new CustomerReturnsModel();
//IList<CustomerReturnItemModel> myList = new List<CustomerReturnItemModel>();
if (ModelState.IsValid)
{
//insert customer return items
model.ProductBrand = form["ProductBrand"];
model.ProductBrandQuantity = Int32.Parse(form["ProductBrandQuantity"]);
model.ProductName = form["ProductName"];
//myList.Add(model);
Model.Items.Add(model);
//return Json(new
//{
// success = true
//});
}
//If we got this far, something failed, redisplay form
return PartialNoAccountWarrantyProductFormView();
}
当最终提交按钮发生时,转到此方法。
[HttpPost]
public ActionResult NoAccountWarrantyFormYourDetails(CustomerReturnsModel model, FormCollection form)
{
CustomerReturns CustomerReturn = new CustomerReturns();//to customer return table
CustomerReturnItems CustomerReturnItem = new CustomerReturnItems();//to the items table
int CustomerReturnID;
if (ModelState.IsValid)
{
CustomerReturn.CustomerEmail = model.CustomerEmail;
CustomerReturn.CustomerFullName = model.CustomerFullName;
CustomerReturn.CustomerPhoneNumber = model.CustomerPhoneNumber;
CustomerReturn.InvoiceNumber = model.InvoiceNumber ?? CustomerReturn.InvoiceNumber;
CustomerReturn.DateSubmitted = DateTime.Now;
CustomerReturnID = _customerReturnsService.InsertCustomerReturn(CustomerReturn);
//insert customer return items
CustomerReturnItem.CustomerReturnID = CustomerReturnID;
CustomerReturnItem.ProductBrand = form["ProductBrand"];
CustomerReturnItem.ProductBrandQuantity = Int32.Parse(form["ProductBrandQuantity"]);
CustomerReturnItem.ProductName = form["ProductName"];
_customerReturnItemsService.InsertCustomerReturnItems(CustomerReturnItem);
var items = model.Items;
foreach (var CustomerReturnItems in items)
{
CustomerReturnItem.CustomerReturnID = CustomerReturnID;
CustomerReturnItem.ProductBrand = CustomerReturnItems.ProductBrand;
CustomerReturnItem.ProductBrandQuantity = CustomerReturnItems.ProductBrandQuantity;
CustomerReturnItem.ProductName = CustomerReturnItems.ProductName;
}
return Json(new
{
success = true,
CustomerReturnId = CustomerReturnID
});
}
//If we got this far, something failed, redisplay form
return View("~/Plugins/Widgets.CustomerReturns/Views/CustomerReturns/CustomerReturnsNoAccount.cshtml");
}
我不确定为什么这不会起作用,看起来如此接近。
我希望这是有道理的。 干杯