1

I am developing an online Hotel Management system in asp[dot]net mvc. I have a list of items and each item has a checkbox in it which is for adding it to the order list whenever it is to be added. Problem is whenever I click "Add To Orders" button I get "error alert". I want Get Method of order controller to be called so that data in it should be shown in the view. Also check whether my post method is right or not?

 <div class="form-group">
     <div class="col-md-offset-2 col-md-2">
        <input value="Add to Orders" class="btn btn-primary"        onclick="submitItems()" />
        @*<input id="submit" value="Place an Order" class="btn btn-   primary" onclick="" />*@
    </div>
</div>
<script>

    //$('#submit').on('click', function (e) {
    //    e.preventDefault();
    var submitItems = function () {

        var arrItems = [];
        var commaSepratedValues = "";
        $("#itemList input[type=checkbox]").each(function (index, val) {
            debugger;
            var checkedId = $(val).attr("id");
            var arr = checkedId.split("_");
            var currentItemId = arr[1];
            var isChecked = $("#" + checkedId).is(":checked", true);
            if (isChecked) {
                arrItems.push(currentItemId);
            }
        })

        if (arrItems.length != 0) {
            commaSepratedValues = arrItems.toString();
            $.ajax({
                url: "/Order/Create",
                type: "GET",
                data: { ItemList: commaSepratedValues },
                success: function (data) {
                    alert('success');
                },
                error: function () { alert('error'); }

            });

        }

    }
</script>

Above is the code where my function executes and then the problem arises.

Main Menu Step 1 Step 2 step 3

er-sho
  • 9,581
  • 2
  • 13
  • 26
  • i checked your screenshots, did u received comma separated string in your controller `Create` action method by adding breakpoint? – er-sho Nov 02 '18 at 13:19
  • Yes @ershoaib I receive comma separated values in controller "Create" Action – waqasqureshi Nov 02 '18 at 13:26
  • add `try/catch` block in your `create` action method and let me know which error or exception did u faced? – er-sho Nov 02 '18 at 13:27
  • Although i have added a breakpoint no exception is occured but an js alert of error has pop up of jquery ajax – waqasqureshi Nov 02 '18 at 13:34
  • something exception is there so that why your ajax call goes to `error: function ()`. try to add `try/catch` block otherwise you can't get rid from this alert – er-sho Nov 02 '18 at 13:36
  • @ershoaib cm on any desk 714 101 912 – waqasqureshi Nov 02 '18 at 13:42
  • is there any exception? – er-sho Nov 02 '18 at 13:47
  • i m not able to take your pc access and keyboard so check that why the create action method hit double i think i can gives you error alert – er-sho Nov 02 '18 at 13:57
  • now you can access my pc cm – waqasqureshi Nov 02 '18 at 14:05
  • Sorry friend i leave my office we'll continue tomorrow if possible :) – er-sho Nov 02 '18 at 15:21
  • @ershoaib Its ok – waqasqureshi Nov 02 '18 at 15:49
  • You have not shown your controller methods or the model or the view, but that is an awful way to go about it. If you want a checked list box, refer [Pass List of Checkboxes into View and Pull out IEnumerable](https://stackoverflow.com/questions/29542107/pass-list-of-checkboxes-into-view-and-pull-out-ienumerable/29554416#29554416). –  Nov 02 '18 at 21:57

0 Answers0