Click here to Skip to main content
15,892,927 members
Articles / Programming Languages / C# 4.0

Simple CRUD Using Backbone.js in ASP.NET MVC Razor

Rate me:
Please Sign up or sign in to vote.
4.66/5 (11 votes)
22 Apr 2012CPOL6 min read 80.6K   4K   35  
Here is a simple CRUD on the MVC music store using backbone.
@model RazorApp.ViewModels.ShoppingCartViewModel
@{
    ViewBag.Title = "Shopping Cart";
}
<script type="text/javascript">
    $(function () {
        var requestUrl = '@Url.Action("GetCart")';

        $("#cartResults").jqGrid({
            url: requestUrl,
            datatype: 'json',
            mtype: 'POST',
            emptyrecords: "No results",
            colNames: ['Album Name', 'Price', 'Quantity', ''],
            colModel: [
                            { name: "Title", width: 350, editable: false, searchoptions: {}, index: "Title" },
                            { name: "Price", width: 100, editable: false, searchoptions: {}, index: "Price" },
                            { name: "Quantity", width: 100, editable: false, searchoptions: {}, index: "Quantity" },
                            { name: 'Delete', width: 140, formatter: deleteFormatter }
                            ],
            pager: $('#cartPager'),
            rowList: [],        // disable page size dropdown
            pgbuttons: false,     // disable page control like next, back button
            viewrecords: false,    // disable current view record text like 'View 1-10 of 100' 
            sortname: 'id',
            sortorder: 'asc',
            loadonce: false,
            sortable: true,
            scrollrows: true,
            height: 300,
            pgtext: null,
            hoverrows: false,
            footerrow: true,
            userDataOnFooter: true,
            emptyDataText: "Your Cart is Empty",
            gridComplete: function () {
                if ($('#cartResults').getGridParam('records') == 0) {
                    //var noRecords = "<div class=\"loading ui-state-default ui-state-active\" id=\"load_forSteps_Home_table\" style=\"left: 0px; top: 48px; display: block;\"><div class=\"field-validation-error\" style=\"padding:12px 4px 4px 4px\">Your Cart is Empty.</div></div>";
                    var noRecords = "<div class=\"customloading\" id=\"load_cart_Results\" style=\"left: 0px; top: 48px; display: block;\"><div class=\"ui-state-highlight ui-corner-all\" style=\"margin-top: 20px; padding: 0 .7em;width: 500px;height:200px\">";
                    noRecords += "<p><span class=\"ui-icon ui-icon-info\" style=\"float: left; margin-right: .3em;\"></span><strong>No records</strong><br /><br /><br />";
                    noRecords += "Your Cart is Empty. Select albums to your cart.</p></div></div>";

                    $('#lui_cartResults', '.ui-jqgrid').after($(noRecords));
                    $('#btnCheckOut').hide();
                } $("button").button();
                $(".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) {
                        $('p', '#update-message-disp').append(data.Message);
                        $('#update-message-disp').show("slow");
                        $('#cartResults').trigger("reloadGrid");
                        return false;
                    });
                    }
                });
            }
        });

        function deleteFormatter(cellvalue, options, rowObject) {
            return "<button  class=\"RemoveLink\" data-id = \"" + options.rowId + "\">Remove from Cart</button>";
        }

        $('#update-message-disp').hide();
    });
</script>
<div class="styler">
    <fieldset class="ui-widget">
        <legend class="ui-state-legend-default ui-corner-top ui-corner-bottom">Review Your Cart</legend>
        <div id="update-message-disp" class="ui-state-highlight ui-corner-all" style="margin-top: 20px; padding: 0 .7em;width: 500px">
            <p><span class="ui-icon ui-icon-info" style="float: left; margin-right: .3em;"></span><strong>Cart modified</strong><br />
            </p>
        </div>
        <br />
        <table id="cartResults">
        </table>
        <div id="cartPager">
        </div>
    </fieldset>
    @Html.ActionLink("Checkout>>", "AddressAndPayment", "Checkout", null, new { id = "btnCheckOut"})
</div>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions