Im trying to loop through a class to get the values of the hidden fields and also the values of any CHECKED checkboxes, I need to loop the class as it my be produced more than once on the page as several partial views may be produced from a shopping cart for product embellishments. I want to then submit the loop via ajax to a MVC controller (ive created the backend stuff allready)
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>
But the HTML could be like this with two separate items
<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"));
});
});
});
I can get a single form easily like below -
$('#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);
}
});
});
Controller
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);
}
ViewModel
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">
? And what is the signature of the POST method and the model you want to bind to?id
attributes - so you should use class names instead. And it does not make much sense that you have the same name for each hidden input