Con risposta getting a field value from an MVC model

  • Wednesday, September 19, 2012 1:58 PM
     
     

    My Sharepoint remote ClientContext application requires a JSONFeed ( also CSV & XML feeds too)  from it. in order to reduce code and aid clarity, I want to put the command to format the JSON into a function.  as follows 

     foreach (MVCHrer.Models.CustomClass oListItem in BigList)
                {
                    json += "{";
                    json += JSONval(oListItem, "myFieldone", " ") + ",";
                    json += JSONval(oListItem, "MyFieldtwo", " ") + ",";

    etc....  

    }

    This works for looping through a ListItemCollection  ( Obviously the foreach loop has different classes) but I can t it to work when navigating a list of  corresponding to an MVC model type.  I want to use this, so that I can apply MVC model validation & add extra fields, but I cant find a form of syntax that works.  

       

    This works

       public string JSONval(sp.ListItem oListItem, string i_field, string nul_val)
            {
                string retval = '"' + i_field + '"' + ":" + '"';
                string nval = "";
                string dataValue = HttpUtility.HtmlEncode(oListItem[i_field].ToString());
                retval += dataValue+'"';
                return (retval);

          }

    This does not . Two basic version of what I have tried are commented out in the function below 

            public string JSONval(MVCHrer.Models.CustomClass MVCListItem, string i_field, string nul_val)

            {
                string retval = '"' + i_field + '"' + ":" + '"';
                string dataValue = "";
       //  string fval = oListItem.GetType().GetField(i_field).GetValue(oListItem).ToString();

    //ver1          

    // oListItem[i_field].ToString();  

    //ver 2

                retval += dataValue + '"';
                return (retval);
            }

    Any suggestions gratefully recieved 

    I have seen references to FieldInfo , But not even intellisense can bring back something that works....

         

       Type myTypeA = Type.GetType(oListItem);
        FieldInfo myFieldInfo = myTypeA.GetField(i_field);

     retval = myFieldInfo.GetValue();


    Richard



All Replies