简体   繁体   中英

jquery Ajax call from view to controller method but not disply the view returned that method in mvc

I have given Ajax call from Index.cshtml view to PrintPreview method which is present in MasterList Controller and i have also passed parameters. The Index method also present in MasterList controller But while returning view from PrintPreview method call is going to PrintPreview.cshtml page but the page is not loading ie not displaying in browser and Index.cshtml page displaying in browser please help.

enter code here

    $('#printbtn').click(function () {
        $.ajax({
            type: 'POST',
            url: '@Url.Action("PrintPreview", "MasterList")',                
            data: 
    { saleorderIdList: JSON.stringify(saleorder_id),
  orderIdList:JSON.stringify(order_id) },

            });
    });





    public ActionResult PrintPreview(string saleorderIdList, string orderIdList)
    {
        var locationIdOfLoginUser = Convert.ToInt32(Session["LocationId"]);
        ViewBag.loccationName = Session["LocationName"];
        ViewBag.locationType = Session["LocationType"];
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        IEnumerable<int> saleOrderIds = new int[] { };
        IEnumerable<int> orderIds = new int[] { };
        if (saleorderIdList != null)
        {
            saleOrderIds = serializer.Deserialize<IEnumerable<int>>(saleorderIdList);
        }
        if (orderIdList != null)
        {
            orderIds = serializer.Deserialize<IEnumerable<int>>(orderIdList);
        }
        MasterListService masterListService = new MasterListService();
        var ordercollection = masterListService.GetSelectedorders(locationIdOfLoginUser, saleOrderIds, orderIds);
        return View(ordercollection);

    }

The data has to match the "shape" expected. You are passing a single object (with 2 properties) but it is expecting a list (ie JSON array).

You should change your post instead to pass an array:

data: passresult: [{ saleorderIdList: JSON.stringify(saleorder_id),
      orderIdList:JSON.stringify(order_id) }],

If that fails, just as a test, change your action to this and see if it is hit (for your existing data):

public ActionResult PrintPreview(MasterListDTO passresult)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM