[英]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:在您的表单上,您有
User
和Role
的下拉列表,并且表单数据将仅包含这两个的 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.