Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Ajax post from jquery results in null at Controller #6438


User avatar
0
aggarwal created

Hi i am following this guidance.

https://aspnetboilerplate.com/Pages/Documents/Javascript-API/AJAX

Problem: null is received in the controller method. It is hitting the right controller method.

Jquery code

                                action: function (data) {
                                    var record = { id: data.record.integration.id };
                                    abp.ajax({
                                        url: '/App/Integrations/Register',
                                        data: JSON.stringify(record),
                                    }).done(function (data) {
                                        abp.message.confirm(
                                            'You will be redirected',
                                            'Are you sure?',
                                            function (isConfirmed) {
                                                if (isConfirmed) {
                                                    window.location = data.url
                                                }
                                            }
                                        )
                                    });
                                }

Model

    public class IdModel
    {
        int id { get; set; }
    }

Controller method

        [HttpPost]
        public JsonResult Register(IdModel id)
        {
            string compCode = id.ToString();
            string url = "";

            if (!string.IsNullOrEmpty(compCode))
            {
                var t = new HmrcOauth()
                {
                    State = compCode
                };

                url = t.GetAuthorizationUrl();
            }

            object json = new { Url = url };
            return Json(json);

        }

Please advice. I have already spent 2 days trying different techniques.

Thanks in advance.

Regards, vikas


11 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team

    Try marking id as public.

    public class IdModel
    {
        public int id { get; set; }
    }
    
  • User Avatar
    0
    aggarwal created

    Hi @aaron,

    I made that change. The object is still coming as null.

    Regards, Vikas

  • User Avatar
    0
    aaron created
    Support Team

    What is the value of console.log(record)?

  • User Avatar
    0
    aggarwal created

    {id: 7}

  • User Avatar
    0
    aggarwal created

    Just renames id to xid to avoid any another conflict. screenshot from console is below.

    Its value is coming as 0 now in controller.

  • User Avatar
    0
    aaron created
    Support Team

    Try renaming id in controller.

  • User Avatar
    0
    aggarwal created

    Renamed everything(controller and scripts) to as below but value for Xid is coming as 0.

            public JsonResult GBVATRegister(IdModel IdModel)
            {
                string compCode = IdModel.Xid.ToString();
                string url = "";
    
  • User Avatar
    0
    aggarwal created

    @aaron

    This is the only issue preventing me to close down this functionality. Please help.

    I also tried adding another string parameter. It is coming as null.

    Hence there seems to be an issue receiving the json object.

  • User Avatar
    0
    aaron created
    Support Team

    TL;DR: Add the [FromBody] attribute to the parameter in your ASP<span></span>.NET Core controller action

    — https://andrewlock.net/model-binding-json-posts-in-asp-net-core/

    - public JsonResult Register(IdModel model)
    + public JsonResult Register([FromBody] IdModel model)
    

    Existing examples in ASP<span></span>.NET Zero: AccountController's Impersonate and SwitchToLinkedAccount methods.

  • User Avatar
    0
    aggarwal created

    It worked :)

    I would buy your a beer right now, if you were here.

    Thank you.

    Cheers, Vikas

  • User Avatar
    0
    aggarwal created

    Issue closed