0

I have written code in an ASP.NET Core 6 controller and calling this from view. This code gives response to my view but I don't know how to parse the data in view.

Previously I was using JsonrequestBehaviour.Allowget which is now deprecated in .NET 6. Please help me for better appraoch of json call which can return any dynamic object.

Here is my controller code:

public IActionResult GetAccountLevelAndCode(Int32 GroupAccountID, Int32 Companyid)
{
    string AccountLevels = ""; string Returnerror; string ReturnBranches;
    DataTable AL = new GetDataClass().GetAccountNoAndAndLevels(GroupAccountID, Companyid, out Returnerror);
    //a = (GLChartOFAccountModel)AL.Rows[0].ConvertDataRowToObject(a);
    string Sql = @"select cab.BranchID from GLChartOFAccount ca inner join GLChartOfAccountBranchDetail cab on ca.GLCAID=cab.GLCAID where cab.GLCAID=" + GroupAccountID;

    DataTable dt = new DataTable();
    dt = StaticClass.SelectAll(Sql).Tables[0];

    AccountLevels = JsonConvert.SerializeObject(AL);
    ReturnBranches = JsonConvert.SerializeObject(dt);

    Returnerror = JsonConvert.SerializeObject(Returnerror);

    return Json(new { AccountLevels, ReturnBranches, Returnerror });
}

Following is my view call and response allocation:

 function GetAccountNoandLevel() {
    var DATA={"GroupAccountID" : $('#isParent').val(), Companyid : @Model.CompanyID }
    var execCode = true;
    $.ajax({
        async: false,
        type: "POST",
        url: "/GLChartOFAccount/GetAccountLevelAndCode",
        data: DATA,
        dataType: "json",
        success: function (data) {
            try {
                var c = JSON.parse(data.AccountLevels)
                var b = JSON.parse(data.ReturnBranches)
                var er = JSON.parse(data.Returnerror)

                if (b.length>0) {

                    $("#BrachIDs option").each(function () {
                        var idParent = $(this).parent().attr("id");
                        this.disabled = true;
                    });
                    var dataarray = '';
                    for (var i = 0; i < b.length; i++) {
                        dataarray += b[i]["BranchID"] + ',';
                    }
                    dataarray = dataarray.replace(/,\s*$/, "");
                    var data = dataarray.split(",");
                    $("#BrachIDs").val(data);
                    $("#BrachIDs").change();
                    if (data.length > 0) {
                        for (var i = 0; i < data.length; i++) {
                            $("#BrachIDs option").filter("[value='" + data[i] + "']").attr('disabled', false);
                        }
                    }
                }
                else {
                    $("#BrachIDs option").each(function () {
                        var idParent = $(this).parent().attr("id");
                        this.disabled = false;
                    });
                    $("#BrachIDs option:selected").removeAttr("selected");
                }

                if (ShowErrorOK(er)) {
                    $('#GLCode').val('');
                }else{
                    RowToFillValues(c)
                }

            } catch (e) {
                //console.log(e + " GetAccountNoandLevel "); document.getElementById("DisplayErrorMessage").innerText = e.message; $('#btnTriggerMessage').click(); execCode = false; return false;
                console.log(e + " GetAccountNoandLevel "); console.log(e.message)
            }
        },
        error: function (err) {
            //console.log(err.responseText); document.getElementById("DisplayErrorMessage").innerText = "AJAX error in request: " + JSON.stringify(err.statusText + " " + err.status) + " GetAccountNoandLevel::Unable To Get Details"; $('#btnTriggerMessage').click(); execCode = false; return false;
            console.log("AJAX error in request: " + JSON.stringify(err.statusText + " " + err.status) + " GetAccountNoandLevel::Unable To Get Details")
        }
    });

    if (execCode) {
        
    }
}

The response data is showing undefined...

6
  • You have to create a partial view, or show us the view you have now. Commented Aug 20, 2022 at 9:13
  • This is My view and on Change of parentID tag value I am Call the Above Mentioned Jquery Function Commented Aug 20, 2022 at 10:16
  • Can't post the code of complete view as it is too lengthy I am just calling a simple ajax call to controller and getting the response back on view. Everything is working fine but the responce which is returned from server side can't parse to json as we used to parse by using JSON.parse. This may be because in .Net 6 jsonrequestbehviour.allowget is not availble so is there any other way that we can get responce through Ajax calls?? Commented Aug 20, 2022 at 10:25
  • Allowget doesn't do anything, forget about it. Commented Aug 20, 2022 at 10:26
  • There is no need of the partial view here. Commented Aug 20, 2022 at 10:26

1 Answer 1

1

you don't need serialize and parse manually, it will be done automaticaly

return new JsonResult(new { AccountLevels=AL, ReturnBranches=dt, Returnerror= Returnerror });

and ajax

var c = data.AccountLevels;
var b = data.ReturnBranches;
var er = data.Returnerror;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.