繁体   English   中英

从 model class 获取 null 值,该值与 Z466DEEC76ECDF5FCA6D38571F63file24D54 绑定

[英]Getting null value from model class which is bind 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.

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 文件

{ 
  "Id": "1",
  "Name": "Kazuma",
  "Age": 21,
  "CityName": "Tokyo",
  "ZipeCode": "123456",
  "CountryName": "Japan"
},

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
}

您必须将 Json 数据转换为 UserInfo object:

UserInfo userInfo = JsonConvert.DeserializeObject<UserInfo>(s);

此数据可用于索引页面。

你怎么称呼action Method? 我在 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM