Home > OS >  Method Overload with Ajax call
Method Overload with Ajax call

Time:08-02

This is my controller

 public ActionResult NPDAdd(NPD npd) //object model 
        {
            _purchaseOrderRepository.NPDInsertUpdate(npd);
            return View();
        }

        public ActionResult NPDAdd(string npd) //string para
        {
            _purchaseOrderRepository.NPDInsertUpdate(npd);
            return View();
        }

This is my Repository

public void NPDInsertUpdate(NPD npd)
        {
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("NPDId", npd.NPDId);
            parameters.Add("FGCode", npd.FGCode);
            parameters.Add("SFGCode", npd.SFGCode);
            parameters.Add("Plant", npd.Plant);
            parameters.Add("M1Qty", npd.M1Qty);
            parameters.Add("M2Qty", npd.M2Qty);
            parameters.Add("M3Qty", npd.M3Qty);
            parameters.Add("M4Qty", npd.M4Qty);
            parameters.Add("M5Qty", npd.M5Qty);
            parameters.Add("M6Qty", npd.M6Qty);
            con.Query<NPD>("usp_NPD_Insert-Update", parameters, commandType: CommandType.StoredProcedure, commandTimeout: CommonConstants.CommandTimeOut).ToList();

        }

public void NPDInsertUpdate(string scm) {

            DynamicParameters para = new DynamicParameters();
            para.Add("data", scm);
            con.Open();
            con.Query<NPD>("usp_NPD_Insert-Update", param: para, commandType: CommandType.StoredProcedure).ToList();
            con.Close();
        }

this is my interface (i followed Dependency Injection)

 void NPDInsertUpdate(NPD npd); //insert update
 void NPDInsertUpdate(string npd);

this is ajax call

$('body').on('click', '#sync', function () {
    debugger;
    if (final.length <= 0) {

        alert("No Changes Done till now");
    }
    else {

        $.ajax({
            type: 'POST',
            url: ROOT   "PurchaseOrder/NPDAdd",
            dataType: "json",
            data: { final: final },
            success: function (i) {

                //console.log(i)
            },
            complete: function (i) {
                window.location.reload();
            }
        });
    }
});
------
$.ajax({
                type: "POST",
                url: ROOT   'PurchaseOrder/NPDAdd',
                data: formdata,
                complete: function () {
                    //load data
                    $.ajax({
                        url: ROOT   "PurchaseOrder/NPDListLoad",
                        type: 'POST',
                        dataType: 'JSON',
                        success: function (dataJson) {
                            npdListData = dataJson;
                            location.reload();
                            $("#npdListGrid").jqGrid().setGridParam({
                                datatype: 'local',
                                data: npdListData
                            }).trigger('reloadGrid');
                            $('#modal-addNPD').modal('hide');
                        }
                    });
                }
            });

here the depend on parameter it should call particular method "NDPAdd" please help me out

parameter can be string or modal

but in ajax itself ,not hitting , please help me out

am trying for long time and not able to solve it

CodePudding user response:

instead you can use the same method and difference the behaviour inside like this:

    public ActionResult NPDAdd([FromBody] JObject json) //object model 
    {
        if (json["npd"] != null)
        {
            if (json["npd"].Type == JTokenType.String)
            {
                string npd = json["npd"].ToString();
                _purchaseOrderRepository.NPDInsertUpdate(npd);
            }
            else
            {
                string npd = json["npd"].ToObject<NPD>();
                _purchaseOrderRepository.NPDInsertUpdate(npd);
            }
        }
        return View();
    }

I've done using https://www.newtonsoft.com/json library but is only for the concept to how to do this.

  • Related