0

I am working with .netcore asp.net project using C# jquery ajax json. I have a list object, Which containing an Designation, Department and EmployeesList. Is there a way to convert from List to ViewModel object?

Emp_Designation_AssignController.cs

[HttpPost]
        public IActionResult InsertDesignation(string formData)
        {
            var rawJSON = formData;            
            var obj = JsonConvert.DeserializeObject<Dictionary<string, object>>(rawJSON);            
            List<object> list = new List<object>();
            list.AddRange(obj.Values);
            Emp_des_ViewModel emp_des_ViewModel = new Emp_des_ViewModel();
            //I tried as below line but unable to convert 
            emp_des_ViewModel = List<T>.Select(x => x as Emp_des_ViewModel ).ToArray();
            //other code 
        }
  • You can deserialize to Model, check this: [Deserialize JSON to C# Classes](https://stackoverflow.com/questions/25052293/deserialize-json-to-c-sharp-classes).... Then you can select and create your ViewModel [Creating a new object in LINQ query](https://stackoverflow.com/questions/35073816/creating-a-new-object-in-linq-query)... Or use Automapper to Map object to your ViewModel [How to use AutoMapper?](https://stackoverflow.com/questions/2717029/how-to-use-automapper) – Sasaman May 13 '21 at 08:19
  • Could you post Emp_des_ViewModel pls? – Serge May 14 '21 at 14:12

1 Answers1

0

The as operator checks if the parameter is the specified type or descends from that type, and if so it returns a typed reference to it. Otherwise it returns null. The type that you're casting from is never going to be either UserRoleViewModel or anything that as will recognize as casting from it, so you're just going to end up with an array of nulls, one for each property in the original JSON data.

The right way to handle this is to deserialize directly to the object type. If the JSON isn't formatted correctly for this you can use an intermediate type to deserialize to, then construct your UserRoleViewModel from that.

Since you haven't provided any details for your model I'll make some stuff up.

Let's say you have a model that looks like this:

class UserRoleViewModel
{
    public int UserID { get; set; }
    public string UserName { get; set; }
    public int RoleID { get; set; }
    public string RoleName { get; set; }
}

On your form you have drop-downs for the User and Role, and the form data will only include the IDs for those two:

var rawJSON = "{\"UserID\":42,\"RoleID\":1}";

You can convert this to an instance of the view model directly:

var model = JsonConvert.DeserializeObject<UserRoleViewModel>(rawJSON);

The above produces an instance of UserRoleViewModel with only the ID fields supplied. Any fields not found in the source JSON string will be left as default, so you'll need to be aware of what is and isn't useful in the model, which will depend on your HTML form configuration.

If instead you have a collection of view model data in the form data (looking something like [{"UserID":1,...},{"UserID:2,...}]) then you can (and in fact must) deserialize the entire collection as an array or List<>:

var models = JsonConvert.DeserializeObject<List<UserRoleViewModel>>(rawJSON);
Corey
  • 15,524
  • 2
  • 35
  • 68