RemoveFromCart result in Open File Dialog

Feb 26, 2011 at 6:22 PM

Hi All,

I am new to MVC and web programming in general. So I hope someone can help me out.

Whenever I try to delete an item from the shopping cart, an Open File Dialog displays indicating an unknown file type. I have added application/json mime type to IIS 7. I've tried several ideas I've found in the discussions here but nothing seems to work. All of the relevant code is below. Can someone help? this has me stopped.

ShoppingCartViewModel.Index.cshtml

@model MvcMusicStore.ViewModels.ShoppingCartViewModel
@{
	ViewBag.Title = "Shopping Cart";
}
<script src="../../Scripts/jquery-1.4.4.min.js" type="text/javascript"></script>
<script src="<%: Url.Content('~/Scripts/jquery.unobtrusive-ajax.min.js') %>" type="text/javascript"></script>

<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("/ShoppingCart/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 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>
<h3>
	<em>Review</em> your cart:</h3>
<p>
	@Html.ActionLink("Create New", "Create")
</p>
	<p class="button">
		@Html.ActionLink("Checkout >>", "AddressAndPayment", "Checkout")
	</p>
	<div id="update-message">
	</div>
	<table>
		<tr>
			<th>Album Name</th>
			<th>Price (each)</th>
			<th>Quantity</th>
			<th></th>
		</tr>
	@foreach (var item in Model.CartItems)
	{
		<tr>
			<td>
				@Html.ActionLink(item.Album.Title, "Details", "Store", new { id = item.AlbumId }, null)
			</td>
			<td>
				@item.Album.Price
			</td>
			<td id="item-count-@item.RecordId">
				@item.Count
			</td>
			<td>
				@*<a href="#" class="RemoveLink" id="@item.RecordId">Remove from cart</a>*@
				@Ajax.ActionLink("Remove from Cart", "RemoveFromCart", new { id = item.RecordId }, new AjaxOptions { OnSuccess = "handleUpdate", HttpMethod="Post" })
			</td>
		</tr>
	}
	<tr>
		<td>Total</td>
		<td></td>
		<td></td>
		<td id="cart-total">@Model.CartTotal</td>
	</tr>
</table>

 Shopping Cart Controller.RemoveFromCart

public ActionResult RemoveFromCart(int id)
		{
			ShoppingCart cart = ShoppingCart.GetCart(this.HttpContext);
			Album albumToRemove = storeDB.Carts.SingleOrDefault(a => a.RecordId == id).Album;
			int itemCount = cart.RemoveFromCart(albumToRemove.AlbumId);
			ShoppingCartRemoveViewModel shoppingCartRemoveViewModel = new ShoppingCartRemoveViewModel
			{
				Message = Server.HtmlEncode(albumToRemove.Title + " has been removed from your cart"),
				CartTotal = cart.GetTotal(),
				CartCount = cart.GetCount(),
				ItemCount = itemCount,
				DeleteId = id
			};
			return Json(shoppingCartRemoveViewModel, JsonRequestBehavior.AllowGet);
		}
Coordinator
Feb 26, 2011 at 6:59 PM

Is there a reason you're running this in IIS as opposed to using the built-in web development server? Just trying to understand the scenario.

Feb 26, 2011 at 7:02 PM

actually I am running this in the Web Dev Server. Adding the Mime type to IIS was a possible "solution" that I ran across online. I didn't think about the fact that it was not actually running in IIS.