简体   繁体   English

如何在 asp.net 核心 mvc ZD7EFA19FBE7D39372FD5ADB60242 中将列表 object 转换为 Viewmodel object?

[英]How can I convert List object to Viewmodel object in asp.net core mvc C#?

I am working with .netcore asp.net project using C# jquery ajax json. 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.我有一个列表 object,其中包含一个名称、部门和员工列表。 Is there a way to convert from List to ViewModel object?有没有办法从 List 转换为 ViewModel object?

Emp_Designation_AssignController.cs 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 
        }

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. as运算符检查参数是指定类型还是从该类型下降,如果是,则返回对它的类型化引用。 Otherwise it returns null .否则返回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 null s, one for each property in the original JSON data.您要转换的类型永远不会是UserRoleViewModel或任何可以识别as从中转换的类型,因此您最终将得到一组null s,每个属性一个对应于原始 JSON数据。

The right way to handle this is to deserialize directly to the object type.处理这个问题的正确方法是直接反序列化为 object 类型。 If the JSON isn't formatted correctly for this you can use an intermediate type to deserialize to, then construct your UserRoleViewModel from that.如果 JSON 的格式不正确,您可以使用中间类型进行反序列化,然后从中构造您的UserRoleViewModel

Since you haven't provided any details for your model I'll make some stuff up.由于您没有为您的 model 提供任何详细信息,因此我会编造一些内容。

Let's say you have a model that looks like this:假设您有一个如下所示的 model:

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:在您的表单上,您有UserRole的下拉列表,并且表单数据将仅包含这两个的 ID:

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

You can convert this to an instance of the view model directly:您可以直接将其转换为视图 model 的实例:

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

The above produces an instance of UserRoleViewModel with only the ID fields supplied.上面生成了一个仅提供 ID 字段的UserRoleViewModel实例。 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.源 JSON 字符串中未找到的任何字段都将保留为默认值,因此您需要了解 model 中哪些是有用的,哪些是无用的,这取决于您的 Z4C4AD5FCA2E7A3F74DBB1CED0038 配置。

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<> :相反,如果您在表单数据中有一组视图 model 数据(看起来像[{"UserID":1,...},{"UserID:2,...}] )那么您可以(实际上必须)将整个集合反序列化为数组或List<>

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 ASP.NET Core 3.1 MVC 中的 ViewModel 与 JavaScript 和 C# 动态添加到列表 - How to dynamically add to list with ViewModel in ASP.NET Core 3.1 MVC with JavaScript and C# C# ASP.NET 核心 MVC 需要 object 参考 - C# ASP.NET Core MVC An object reference is required 我们可以在c#asp.net mvc app中使用静态方法将DB对象设置为VIewModel对象吗 - Can we have static method to set DB object to VIewModel object in c# asp.net mvc app C# - ASP.NET MVC - 将对象列表转换为字符串列表,以在会话中使用 - C# - ASP.NET MVC - Convert a List of object , to a List of string, to use in a Session Blazor / C# / ASP.NET 核心 MVC:在 object 中添加一个列表 - Blazor / C# / ASP.NET Core MVC: adding a list inside an object 我们可以将ViewModel分配给ASP.Net MVC中的ViewData对象吗? - Can we assign a ViewModel to a ViewData object in ASP.Net MVC? 如何使用JSON.NET(C#/ ASP.NET)遍历列表并将其转换为JSON对象 - How to iterate through a List and convert it to a JSON object using JSON.NET (C# / ASP.NET) 如何从 asp.net 核心 c# 中的 object 列表中提取键和值 - how to extract keys and values from a list of object in asp.net core c# 如何在 asp.net 核心 1.0 c# 中将字符串(实际上是位图)转换为 IFormFile - How can I convert String (which is actually Bitmap) to IFormFile in asp.net core 1.0 c# 如何在ASP.NET MVC中将下拉选择转换为自定义对象? - How can I convert a dropdown selection to a custom object in ASP.NET MVC?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM