Click here to Skip to main content
16,019,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?)

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


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

so expected result will be

ownerfileno=1222
departementCode=211


What I have tried:

[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();
          
        }
Posted
Updated 24-Mar-23 23:22pm

To add to what Richard has said, JSON data does not "magically" create classes for you, you need to construct a class with the appropriate properties that the JSON can be typed to. I used an online JSON converter[^] and generated this:
C#
public class MyClass
{
    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; }
}
You would then use that as the type in your code:
ValueKind = MyClass : "{"employeeID":1222,"employeeName":0,"departementCode":"211","jobCode":"1221","ownerType":null,"departementName":"ahmed"}"
 
Share this answer
 
The object class is the base of all classes and does not contain either of the requested properties. You must use a properly created (and designed) class that defines the properies.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900