Open Closed

Ajax post from jquery results in null at Controller #6438


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)
  • 0
    aaron created

    Try marking id as public.

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

    Hi @aaron,

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

    Regards, Vikas

  • 0
    aaron created

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

  • 0
    aggarwal created

    {id: 7}

  • 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.

  • 0
    aaron created

    Try renaming id in controller.

  • 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 = "";
    
  • 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.

  • 0
    aaron created

    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.

  • 0
    aggarwal created

    It worked :)

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

    Thank you.

    Cheers, Vikas

  • 0
    aggarwal created

    Issue closed