如何收集的名称,并存储在一个模型的列表在c#MVC的值 [英] how to collect the name and the value stored in a model to a list in c# mvc

查看:92
本文介绍了如何收集的名称,并存储在一个模型的列表在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.

<一个href=\"http://stackoverflow.com/questions/19779502/how-to-get-property-name-and-value-from-generic-model-with-generic-list\">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.


  1. 更改返回类型为JSON

  2. 看看使用字典来构建你的数据。这可能是有用的,以创建一个对象类型重新present的新老用户一起

  1. Change the return type to JSON
  2. 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的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆