【发布时间】:2018-09-13 09:13:36
【问题描述】:
我试图遍历一个类以获取隐藏字段的值以及任何 CHECKED 复选框的值,我需要循环该类,因为它可能会在页面上多次生成,因为可能有几个部分视图从购物车生产的产品装饰。然后我想通过 ajax 将循环提交到 MVC 控制器(我已经创建了后端的东西)
HTML
<div class="EmbPosWrap">
<input class="hidden-field" id="CartItemId" name="hiddenfield" value="167" type="hidden"/>
<input class="hidden-field" id="StoreId" name="hiddenfield" value="1" type="hidden"/>
<input class="hidden-field" id="CustomerId" name="hiddenfield" value="1" type="hidden"/>
<input class="hidden-field" id="ItemID" name="hiddenfield" value="11976" type="hidden"/>
<div class="EmbPosBx">
<input type="checkbox" name="embellishmentcart" value="1" />
<input type="checkbox" name="embellishmentcart" value="2" />
<input type="checkbox" name="embellishmentcart" value="5" />
<input type="checkbox" name="embellishmentcart" value="6" />
</div>
</div>
但是 HTML 可能是这样的,有两个单独的项目
<div class="EmbPosWrap">
<input class="hidden-field" id="CartItemId" name="hiddenfield" value="167" type="hidden"/>
<input class="hidden-field" id="StoreId" name="hiddenfield" value="1" type="hidden"/>
<input class="hidden-field" id="CustomerId" name="hiddenfield" value="1" type="hidden"/>
<input class="hidden-field" id="ItemID" name="hiddenfield" value="11976" type="hidden"/>
<div class="EmbPosBx">
<input type="checkbox" name="embellishmentcart" value="1" />
<input type="checkbox" name="embellishmentcart" value="2" />
<input type="checkbox" name="embellishmentcart" value="5" />
<input type="checkbox" name="embellishmentcart" value="6" />
</div>
</div>
<div class="EmbPosWrap">
<input class="hidden-field" id="CartItemId" name="hiddenfield" value="168" type="hidden"/>
<input class="hidden-field" id="StoreId" name="hiddenfield" value="1" type="hidden"/>
<input class="hidden-field" id="CustomerId" name="hiddenfield" value="1" type="hidden"/>
<input class="hidden-field" id="ItemID" name="hiddenfield" value="1256" type="hidden"/>
<div class="EmbPosBx">
<input type="checkbox" name="embellishmentcart" value="1" />
<input type="checkbox" name="embellishmentcart" value="2" />
<input type="checkbox" name="embellishmentcart" value="3" />
<input type="checkbox" name="embellishmentcart" value="4" />
<input type="checkbox" name="embellishmentcart" value="5" />
<input type="checkbox" name="embellishmentcart" value="6" />
</div>
</div>
jQuery
$(function(){
var items=$(".EmbPosWrap")
$.each(items,function (index,item) {
alert($(item).attr("value"));
var checkboxValues = [];
$('input[name=embellishmentcart]:checked').map(function () {
checkboxValues.push($(this).val());
alert($(item).attr("checkboxValues"));
});
});
});
我可以像下面这样轻松获得一个表单 -
$('#submit').on('click', function () {
var checkboxValues = [];
$('input[name=embellishmentcart]:checked').map(function () {
checkboxValues.push($(this).val());
});
var dataRow = {
'CartItemId': $('#CartItemId').val(),
'embellishmentcart': checkboxValues,
'StoreId': $('#StoreId').val(),
'CustomerId': $('#CustomerId').val(),
'ItemID': $('#ItemID').val()
};
const data = JSON.stringify(dataRow);
console.log(data);
$.ajax({
type: "POST",
url: '@Url.Action("EmbellishmentOrder", "EmbellishmentCart")',
dataType: 'json',
data: dataRow,
success: function (data) {
setTimeout(function () {
window.location.href = data;
}, 2000);
}
});
});
控制器
public ActionResult EmbellishmentOrder(EmbellishmentCartDetailModelVM.EmbellishmentCartDetailItemModelVM vm)
{
var picId = (int)TempData["RecordId"];
foreach (var item in vm.embellishmentcart)
{
EmbellishmentOrderDetailRecord dataModel = new EmbellishmentOrderDetailRecord();
dataModel.CustomerID = vm.CustomerId;
dataModel.StoreID = vm.StoreId;
dataModel.CartItemID = vm.CartItemId;
dataModel.ItemID = vm.ItemID;
dataModel.PictureId = picId;
dataModel.EmbellishmentPositionProductDetailID = item;
_orderDetailService.InsertEmbellishmentOrderDetailRecord(dataModel);
}
return Json(Url.RouteUrl("ShoppingCart"), JsonRequestBehavior.AllowGet);
}
视图模型
namespace Nop.Plugin.Other.ProductEmbellishment.Models.ViewModels
{
public partial class EmbellishmentCartDetailModelVM : BaseNopModel
{
public EmbellishmentCartDetailModelVM()
{
Items = new List<EmbellishmentCartDetailItemModelVM>();
}
public bool ShowSku { get; set; }
public bool ShowProductImages { get; set; }
public bool IsEditable { get; set; }
public IList<EmbellishmentCartDetailItemModelVM> Items { get; set; }
public partial class EmbellishmentCartDetailItemModelVM : BaseNopEntityModel
{
public EmbellishmentCartDetailItemModelVM()
{
Picture = new PictureModel();
}
public PictureModel Picture { get; set; }
public int CustomerId { get; set; }
public int StoreId { get; set; }
public int CartItemId { get; set; }
public int Qty { get; set; }
public string AttributeInfo { get; set; }
public string PictureURL { get; set; }
public string ImageUrl { get; set; }
public string Title { get; set; }
public HttpPostedFileBase File { get; set; }
public int[] embellishmentcart { get; set; }
public int ItemID { get; set; }
public class EmbellishmentPictureModelVM
{
public int Id { get; set; }
public string EmbellishmentPositionDescription { get; set; }
public string EmbellishmentPositionCost { get; set; }
public string ImageUrl { get; set; }
public string Title { get; set; }
public string AlternateText { get; set; }
public int ItemID { get; set; }
}
}
}
}
【问题讨论】:
-
你是说你可以拥有多个
<div class="EmbPosWrap">? POST 方法的签名和要绑定的模型是什么? -
我已经用更多细节更新了帖子,现在我添加类和控制器操作
-
根据您的编辑,您的 html 无效 - 重复的
id属性 - 因此您应该改用类名。而且每个隐藏的输入都使用相同的名称并没有多大意义 -
你到底为什么要使用 ajax 来重定向? (ajax 的重点是保持在 same 页面上)
-
最终结果是完成后停留在同一页面,然后在全部添加后进入购物车并上传图片进行点缀。
标签: jquery html asp.net-mvc