[英]Getting null value from model class which is bind with a json file
I have a c# model class which I have bonded with a json file.我有一个 c# model class 与 Z466DEEC76ECDF5FCA6D38571F6324D5Z4 绑定。 I am using that c# model class but I am getting null values even though I have populated the json file with data.
I am using that c# model class but I am getting null values even though I have populated the json file with data.
C# model class C# model class
public class UserInfo
{
public string Name { get; set; }
public int Age { get; set; }
public string CityName { get; set; }
public string ZipCode { get; set; }
public string CountryName { get; set; }
}
Json file Json 文件
{
"Id": "1",
"Name": "Kazuma",
"Age": 21,
"CityName": "Tokyo",
"ZipeCode": "123456",
"CountryName": "Japan"
},
Asp.Net Core MVC Controller Asp.Net Core MVC Controller
public IActionResult Index(int? page, Models.UserInfo userInfo)
{
return DisplayListView(page, string.Empty, "Index", userInfo); // the "userinfo" is giving me null value
}
You have to convert Json data to UserInfo object:您必须将 Json 数据转换为 UserInfo object:
UserInfo userInfo = JsonConvert.DeserializeObject<UserInfo>(s);
This data can be used in Index page.此数据可用于索引页面。
how you call the action Method?你怎么称呼action Method? I tried that in Ajax then it worked.
我在 Ajax 中尝试过,然后它起作用了。
$.ajax({
type: "POST",
url: "@Url.Action("Index", "ControllerName")",
data: { Id: "1",
Name: "Kazuma",
Age: 21,
CityName: "Tokyo",
ZipeCode: "123456",
CountryName: "Japan" },
dataType: "json",
success: function (response) {
...//Do What you want
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.