我正在通过Json检索一些值,然后将它们转换为对象。接下来我想通过DBAccess.cs脚本将它们传递给数据库。这是通过MVC完成的。我想要的只是获取存储在该对象中的值
这是controller.cs:
public IHttpActionResult PostRegister([FromBody] dynamic register)
{
try
{
Newtonsoft.Json.Linq.JObject employeeRes = (Newtonsoft.Json.Linq.JObject)register.employeedetails;
var employee = employeeRes.ToObject<Employee>();
var inserted_id = uMgt.insertRegDetail(employeeRes);
int nid = (int)inserted_id;
return Ok("success");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return Ok("fail");
}
}
这是userManagement.cs
public int insertRegDetail(Employee emp)
{
try
{
var new_id = db.PassRegistraiondetails("new_insert_detail", emp.Fname, emp.Lname, Convert.ToDateTime(emp.Date_of_Birth), emp.Nic, emp.Gender, emp.Email, emp.Mobile_no, Convert.ToInt32(emp.Department_name), emp.Designation, Convert.ToDateTime(emp.Date_of_join));
return (new_id);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
return (0);
}
}
最后是DBAccess.cs
public int PassRegistraiondetails(string spName, string FName, string LName, DateTime dob, string nic, string Gender, string email, int tel, int id, string Designation, DateTime doj)
{
try
{
SqlCommand cmd = new SqlCommand();
SqlParameter param;
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = spName;
param = new SqlParameter("@fname", SqlDbType.NChar);
param.Value = FName;
param.IsNullable = true;
cmd.Parameters.Add(param);
param = new SqlParameter("@lname", SqlDbType.NChar);
param.Value = LName;
param.IsNullable = true;
cmd.Parameters.Add(param);
param = new SqlParameter("@DOB", SqlDbType.Date);
param.Value = dob;
param.IsNullable = true;
cmd.Parameters.Add(param);
param = new SqlParameter("@gender", SqlDbType.NChar);
param.Value = Gender;
param.IsNullable = true;
cmd.Parameters.Add(param);
param = new SqlParameter("@email", SqlDbType.NChar);
param.Value = email;
param.IsNullable = true;
cmd.Parameters.Add(param);
param = new SqlParameter("@mobile_no", SqlDbType.Int);
param.Value = tel;
param.IsNullable = true;
cmd.Parameters.Add(param);
param = new SqlParameter("@designation", SqlDbType.VarChar);
param.Value = Designation;
param.IsNullable = true;
cmd.Parameters.Add(param);
param = new SqlParameter("@date_of_join", SqlDbType.Date);
param.Value = doj;
param.IsNullable = true;
cmd.Parameters.Add(param);
param = new SqlParameter("@nic", SqlDbType.NChar);
param.Value = nic;
param.IsNullable = true;
cmd.Parameters.Add(param);
param = new SqlParameter("@dept_id", SqlDbType.Int);
param.Value = id;
param.IsNullable = false;
cmd.Parameters.Add(param);
cmd.Parameters.Add("@new_id", SqlDbType.Int).Direction = ParameterDirection.Output;
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
this.OpenConnection();
cmd.ExecuteNonQuery();
int ID = Convert.ToInt32(cmd.Parameters["@new_id"].Value);
this.CloseConnection();
return ID;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return (0);
}
}
我现在得到的错误是:
无法将Newtonsoft.Json.Linq.JObject转换为Doman.Employee
&lt; - 此处域名是基类
我该如何解决这个问题。 Hep非常感谢
这是我的JSON数据
{ "employeedetails":
{"Fname":"awad","Lname":"adadad","Date_of_Birth":"09/13/2016",
"Nic":"asdasdasd","Gender":"Male",
"Email":"asQsa@c","Mobile_no":"1234234234",
"Designation":"asdasd","Date_of_join":"09/27/2016",
"Department_name":"1"
}
}
这是我的Employee.cs
namespace Efutures.HR.LeaveManagement.Domain
{
public class Employee
{
public string Empid { get; set; }
public string Fname { get; set; }
public string Lname { get; set; }
public string Date_of_Birth { get; set; }
public string Gender { get; set; }
public string Email { get; set; }
public int Mobile_no { get; set; }
public string Designation { get; set; }
public string Date_of_join { get; set; }
public string Nic { get; set; }
public string Department_name { get; set; }
}
}
答案 0 :(得分:3)
Ashane Alvis,如果您看到JSON,那么它会有一个名为employeedetails
的属性,您可以在其中包含包含员工信息的对象。
所以你需要一个类让我们说Employee
有一个名为employeedetails
的属性,它的类型为EmployeeDetails
,它在主属性{{1}中具有其余的属性在给定的JSON中。
检查以下代码段,它会将您提供的JSON转换为员工对象。
employeedetails