error when try to get values from object parameter 'object' does not contain a definition for 'employeeID'

Ahmed Salah Abed Elaziz 390 Reputation points
2023-03-25T06:27:34.5266667+00:00

I working on blazor web application server side by csharp . I face issue i can't get values

of properties employee id and department code from object parameter to insert value on table

I get compile time error when try to get value for property employee ID as below

error CS1061: 'object' does not contain a definition for 'employeeID' and no accessible extension method 'employeeID' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

when check owner object after break point hit and data passed as below :

ValueKind = Object : "{"employeeID":1222,"employeeName":0,"departementCode":"211","jobCode":"1221","ownerType":null,"departementName":"ahmed"}"

[HttpPost]
        public ActionResult AddOwnerFile(object owner)
        {
          
            string ownerfileno = owner.employeeID;
            string departementCode = owner.departementCode;

            var sql = "INSERT INTO Owner (OwnerFileNo, DepartementCode) VALUES ({0}, {1})";
            var rowsAffected = _context.Database.ExecuteSqlRaw(sql, ownerfileno, departementCode);

           
            return Ok();
          
        }
so expected result will be

ownerfileno=1222
departementCode=211
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,157 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,385 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,234 questions
{count} votes

2 answers

Sort by: Most helpful
  1. AgaveJoe 26,191 Reputation points
    2023-03-25T18:22:18.9933333+00:00

    The owner parameter is defined as an object type. The object type does not have an employeeID. Create a custom type.

    public class Employee
    {
        public int employeeID { get; set; }
        public int employeeName { get; set; }
        public string departementCode { get; set; }
        public string jobCode { get; set; }
        public object ownerType { get; set; }
        public string departementName { get; set; }
    }
    
    0 comments No comments

  2. Tiny Wang-MSFT 1,571 Reputation points Microsoft Vendor
    2023-03-27T04:29:33.8833333+00:00

    Hi Ahmed, you can certainly convert the object to the model you already/prepare to create like what AgaveJoe said, and I just want to add another way which to read object content directly. My code and test result is like below.

    Object obj = new {
                    EmployeeInfoId = 1,
                    SignatureDataUrl = "hello"
                };
    var props = obj.GetType().GetProperties().First(o => o.Name == "EmployeeInfoId").GetValue(obj, null);
    var prop3 = obj.GetType().GetProperty("EmployeeInfoId").GetValue(obj, null);
    
    var jsonStr = "{\"EmployeeInfoId\":1, \"SignatureDataUrl\":\"url\"}";    
    var data = System.Text.Json.JsonSerializer.Deserialize<EmployeeInfo>(jsonStr);
    var eId = data.EmployeeInfoId;
    JObject converted = Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(jsonStr);
    var eId2 = converted.GetValue("EmployeeInfoId");
    
    
    
    public class EmployeeInfo
    {
        public int EmployeeInfoId { get; set; }
        public string SignatureDataUrl { get; set; }
    }
    
    

    User's image

    In my code above, the Json string is not equal to the result of obj.ToString(), so I create a new Json string for test.

    0 comments No comments