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
Try marking
id
aspublic
.public class IdModel { public int id { get; set; } }
-
0
Hi @aaron,
I made that change. The object is still coming as null.
Regards, Vikas
-
0
What is the value of
console.log(record)
? -
0
{id: 7}
-
0
-
0
Try renaming
id
in controller. -
0
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
@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
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
'sImpersonate
andSwitchToLinkedAccount
methods. -
0
It worked :)
I would buy your a beer right now, if you were here.
Thank you.
Cheers, Vikas
-
0
Issue closed