我正在使用 Angular 7 开发前端的网站。我正在将一个 json 格式的 angular 数组发送到 c# asp.net 核心。我正在获取以下格式的数据
`First = {"model": [{
"_organization_Name": "erw",
"_designation": "dfs",
"_p_Start_Date": "2019-02-28T19:00:00Z",
"_p_End_Date": "2019-03-27T19:00:00Z"
},
{
"_organization_Name": "erwfg",
"_designation": "dfsfgfg",
"_p_Start_Date": "2019-02-28T19:00:00Z",
"_p_End_Date": "2019-03-27T19:00:00Z"
}
]
}`
我在下面的asp.net核心中的方法
[HttpPost("[action]")]
public IActionResult SaveProfessional([FromBody]JObject prof)
{
var obj = new Candidate_Professional_Info_Table(_context);
obj.Identity_Number = 112131.ToString();
obj =
JsonConvert.DeserializeObject<Candidate_Educational_Info_Table>(prof);
var result = obj.SaveProfessional(prof);
return Ok(new { suces = "result" });
}
我的代码没有反序列化 json 对象请提供任何解决方案,因为现在我在这个问题上花了一个星期。下面给出了我的 c# 模型
public int ID { get; set; }
public string Identity_Number { get; set; }
[Required]
[RegularExpression("^[a-zA-Z0-9]+$", ErrorMessage = "Organization Name Not Valid")]
public string Organization_Name { get; set; }
[Required]
[RegularExpression("^/d{1, 2}///d{1,2}///d{4}$")]
public DateTime? p_Start_Date { get; set; }
[RegularExpression("^/d{1, 2}///d{1,2}///d{4}$")]
public DateTime? p_End_Date { get; set; }
[Required]
[RegularExpression("^[a-zA-Z0-9]+$", ErrorMessage = "Designation Name Not Valid")]
public string Designation { get; set; }
public bool InProgress { get; set; }
[NotMapped]
public List<Candidate_Professional_Info_Table> listprof { get; set; }
[NotMapped]
private readonly RegConnString _Context;
public Candidate_Professional_Info_Table(RegConnString connString)
{
_Context = connString;
}
您的属性前有下划线,这就是 JsonConvert 无法正确映射的原因。
您可以使用 [JsonProperty("")] 属性映射到正确的属性
你的属性看起来像那时
[Required]
[RegularExpression("^[a-zA-Z0-9]+$", ErrorMessage = "Organization Name Not Valid")]
[JsonProperty("_organization_Name")]
public string Organization_Name { get; set; }
[Required]
[RegularExpression("^/d{1, 2}///d{1,2}///d{4}$")]
[JsonProperty("_p_Start_Date")]
public DateTime? p_Start_Date { get; set; }
[RegularExpression("^/d{1, 2}///d{1,2}///d{4}$")]
[JsonProperty("_p_End_Date")]
public DateTime? p_End_Date { get; set; }
[Required]
[RegularExpression("^[a-zA-Z0-9]+$", ErrorMessage = "Designation Name Not Valid")]
[JsonProperty("_designation")]
public string Designation { get; set; }
剩下的来处理。您有一个名为模型的根对象。但我们想要的只是List<Candidate_Educational_Info_Table>
public IActionResult SaveProfessional([FromBody]JObject prof)
{
//select the model itself. Also do some nullchecking to prevent errors if empty
var fooModel = prof.First;
//select the content of the model(your actual array). Also do some nullchecking to prevent errors if empty
var fooObjectArray= fooModel.First;
var theDeserializedList = fooObjectArray.ToObject<List<Candidate_Educational_Info_Table>>();
foreach (var item in theDeserializedList)
{
//handle the rest of what you want to do.
//As i see you inject the context into the objects on create but this will not be possible if you use the deserialze.
//Its better to pass the context on the method SaveProfessional
}
return Ok(new { suces = "result" });
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句