I am working on shopping cart using asp.net mvc .I have made complete functionality of shopping cart where user can remove product or update its quantity via text box. But i don't like the part where i have 2 extra links on the right side of each product in cart (update quantity and remove product).I am fine with remove product links cos it makes sense to have remove link for each product. Now i want to have only one button or link called save changes. On click on that button/link i want to gather values from each quantity text box ,pass it to controller and do what its need to be done and return as json object from controller. Problem is that i use ajax for few days only so far and don't know how to iterate/gather values from text boxes in my view and send them to controller.
This is how my shopping cart looks like.
This is my view
@model OnlineShop.ViewModels.ShopingCartViewModel
....
<p class="button">
@Html.ActionLink("Checkout >>", "AddressAndPayment", "Checkout")
</p>
<table class="table-bordered">
<tr>
headers
</tr>
@for (int i = 0; i < Model.CartItems.Count; i++)
{
<tr id="row-@Model.CartItems[i].RecordID">
<td>
<img src="@Url.Content(Model.CartItems[i].Product.ProductImg)" class="img-responsive" style="width:60px;height:60px;padding:5px;"/>
</td>
@*need to be done later*@
<td>
@Html.ActionLink(Model.CartItems[i].Product.ProductName, "Details", "Store", new{id =Model.CartItems[i].ProductID}, null)
</td>
<td>
@Model.CartItems[i].Product.ProductPrice
</td>
<td>
@Html.TextBoxFor(model=>model.CartItems[i].CartCount,new { style="width:60px;text-align:center;"})
</td>
<td>
<a href="#" class="UpdateLink" data-id="@Model.CartItems[i].RecordID" txt-id="CartItems_@(i)__CartCount">
Update Quantity
</a> |
</td>
<td>
<a href="#" class="RemoveLink" data-id="@Model.CartItems[i].RecordID">
Remove from
cart
</a>
</td>
</tr>
}
<tr>
<td>
</td>
<td></td>
<td></td>
<td></td>
<td align="center"> <b>Total</b></td>
<td id="cart-total" align="center">
<b> @Model.CartTotal</b>
</td>
</tr>
</table>
</div>
Scripts
@section scripts {
<script type="text/javascript">
$(function () {
// Document.ready -> link up remove event handler
$(".RemoveLink").click(function () {
// Get the id from the link
var recordToDelete = $(this).attr("data-id");
if (recordToDelete != '') {
// Perform the ajax post
$.post("/ShopingCart/RemoveFromCart", { "id": recordToDelete }, function (data) {
// Successful requests get here
// Update the page elements
if (data.ItemCount == 0) {
$('#row-' + data.DeleteID).fadeOut('slow');
} else {
$('#item-count-' + data.DeleteID).text(data.ItemCount);
}
$('#cart-total').text(data.CartTotal);
$('#update-message').text(data.Message);
$('#cart-status').text('Cart (' + data.CartCount + ')');
});
}
});
});
$(function () {
// Document.ready -> link up remove event handler
$(".UpdateLink").click(function () {
// Get the id from the link
var recordToUpdate = $(this).attr("data-id");
var countToUpdate=$("#" + $(this).attr("txt-id")).val();
if (recordToUpdate != '') {
// Perform the ajax post
$.post("/ShopingCart/UpdateCartQuantity", { "id": recordToUpdate, "cartCount": countToUpdate}, function (data) {
// Successful requests get here
// Update the page elements
if (data.ItemCount == 0) {
$('#row-' + data.DeleteID).fadeOut('slow');
} else {
$('#item-count-' + data.DeleteID).text(data.ItemCount);
}
$('#cart-total').text(data.CartTotal);
$('#update-message').text(data.Message);
$('#cart-status').text('Cart (' + data.CartCount + ')');
});
}
});
});
function handleUpdate() {
// Load and deserialize the returned JSON data
var json = context.get_data();
var data = Sys.Serialization.JavaScriptSerializer.deserialize(json);
// Update the page elements
if (data.ItemCount == 0) {
$('#row-' + data.DeleteID).fadeOut('slow');
} else {
$('#item-count-' + data.DeleteID).text(data.ItemCount);
}
$('#cart-total').text(data.CartTotal);
$('#update-message').text(data.Message);
$('#cart-status').text('Cart (' + data.CartCount + ')');
}
</script>
}
Thank you for hard work,but that confuses me even more :) ...I'll try to explain better what i am trying to achieve ...this is my controller method
[HttpPost]
public ActionResult UpdateCartQuantity(int id , int cartCount)
{
var cart = ShopingCart.GetCart(this.HttpContext);
string productName = storeDB.Carts.Single(
item => item.RecordID == id).Product.ProductName;
Product product = storeDB.Carts.Single(
item => item.RecordID == id).Product;
int oldCount = (int)storeDB.Carts.Single(
item => item.RecordID == id).CartCount;
int itemCount = 0;
if (oldCount==cartCount)
{
itemCount = oldCount;
}
else if (cartCount>oldCount)
{
itemCount = oldCount;
int sub = cartCount-oldCount;
for (int i = 0; i < sub; i++)
{
cart.AddToCart(product);
itemCount++;
}
}
else
{
itemCount = (int)cart.UpdateCartCount(id, cartCount);
}
var results = new ShopingCartRemoveViewModel
{
Message = Server.HtmlEncode(productName) +
" has quantity updated.",
CartTotal = cart.GetTotal(),
CartCount = (int)cart.GetCount(),
ItemCount = itemCount,
DeleteID = id
};
return Json(results);
}
So i was looking at some tutorials and thinking to use <input type="text">
field instead of @html.TextBoxFor(model=>model.CartItems[i].CartCount)
and $ajax()
to post only recordID and value from each <input>
...I could do that maybe for one cartitem with unique name/id or class but considering that i have list of CartItems i need to dynamically make <input>
quantity fields with for statement for each cartitem .So when user press save changes button it needs to read values from each input field and process with method UpdateCartQuantity along with recordID. Main problem for me is how to deal with classes/id's of that <input>
dynamically so each has unique id and how to read/send that recordID and input value. Also i edited my view a bit now i have for statement instead of foreach like u can see.