如何收集的名称,并存储在一个模型的列表在c#MVC的值

编程入门 行业动态 更新时间:2024-10-11 03:22:04
本文介绍了如何收集的名称,并存储在一个模型的列表在c#MVC的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

数据来自绑定模型作为参数传递给我的功能。我需要做的就是收集模型内部的属性到一个列表,我已经做了的名字。此外,我需要存储自带绑定模型到一个列表中的值,然后添加这些值,并将其转换成JSON,这样我可以把它加载到网容易。我的问题是我不能够收集自带在列表中的模型绑定的值。

The data comes bound in the model as parameters to my function. What I need to do is collect the name of the properties inside the model into a list, which I have done already. Also I need to store the values that comes bound in the model into a list, then add these values and convert it into json so that I can load it into grid easily. My problem is I am not being able to collect the values that comes bound in the model in a list.

这是我的code。

public List<string> GetPropertiesNameOfClass(UserModel olduser,UserModel newuser) { List<string> propertyNameList = new List<string>(); List<string> propertyOldValue = new List<string>(); List<string> propertyNewValue = new List<string>(); if (olduser != null) { foreach (var prop in olduser.GetType().GetProperties()) { propertyNameList.Add(prop.Name); // propertyOldValue = prop.GetValue(olduser, null); //var value = prop.GetValue(this) ; } } if(newuser !=null) { foreach(var prp in newuser.GetType().GetProperties()) { //propertyNewValue = prp.GetValue(this); } } // return propertyList + propertyOldValue + propertyNewValue; }

我得到的属性名称通过列表 propertyNameList.Add(prop.Name);但值我无法知道如何将它们绑定在列表中。请帮忙。我已经访问过的链接,但不能在我自己的实现。

I get the list of property names through propertyNameList.Add(prop.Name); but for values I couldn't get idea how to bind them in a list. Please help. I visited this link already but couldn't implement on my own.

How从与泛型列表通用模型获取属性的名称和值?

推荐答案

好吧,我认为这可能是你正在尝试做的,但它是很难知道没有看到您的模型和预期的输出格式正确。

Ok I think this might be what you are trying to do but it is hard to know without seeing your model and your expected output properly formatted.

public List<string> GetPropertiesNameOfClass(UserModel olduser, UserModel newuser) { var propertyNameList = new List<string>(); var propertyOldValue = new List<object>(); var propertyNewValue = new List<object>(); foreach (var prop in olduser.GetType().GetProperties()) { propertyNameList.Add(prop.Name); propertyOldValue.Add(prop.GetValue(olduser, null)); } if(newuser !=null) { foreach(var prp in newuser.GetType().GetProperties()) { propertyNewValue.Add(prp.GetValue(olduser, null)); } } /* Convert To JSON Here */ return formattedJson; }

当你将项目添加到列表中,你不能使用你需要使用添加()为对象列表的单个对象或的AddRange()的+ opperator。

When you are adding an item to a list you can't use the + opperator you need to use Add() for a single object or AddRange() for a list of objects.

的GetValue返航的对象。出于某种原因的ToString没有GetValue返回的对象上工作。

GetValue was returning an object so I changed the the lists that you were adding to the type List. For some reason ToString didn't work on the object returned by GetValue.

一些指点你的下一个步骤。

Some pointers for your next steps.

  • 更改返回类型为JSON
  • 看看使用字典来构建你的数据。这可能是有用的,以创建一个对象类型重新present的新老用户一起

  • Change the return type to JSON
  • Look at using a Dictionary to structure your data. It might be useful to create an Object type to represent an old and new user together public class UserHistory { UserModel oldUser {get; set} UserModel newUser {get; set} }

  • 如果您可以提供围绕如何构建数据的详细信息,我可以再帮你。

    If you can provide more information around how to structure your data I can help you out further.

    更多推荐

    如何收集的名称,并存储在一个模型的列表在c#MVC的值

    本文发布于:2023-10-12 04:07:54,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1483677.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:模型   名称   列表   MVC

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!