locked
getting a field value from an MVC model RRS feed

  • Question

  • User369345748 posted

    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



    Thursday, September 20, 2012 3:37 AM

Answers

  • User-1280676833 posted

    You should get the model type first, like this:

    string Datavalue=rowitem.GetType().GetField("f2").GetValue().ToString();    

    Hope this helpful
    Regards

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 26, 2012 2:27 AM

All replies

  • User197322208 posted

    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

    this is the way that MVC expects a list of items

    http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

    Thursday, September 20, 2012 4:46 AM
  • User369345748 posted

    Thanks for the reply  & the link is interesting.  but I am not entirely sure how to use it to get the specific syntax I need

    Supposing ClassX is made up 2 fields :

    string f1 

    string f2

    ....  it could have more

    & I  already have a list of type "ClassX" called "biglist",  I need the syntax in the foreach loop to get the value of the field "f2" for the currently selected rowitem in the variable Datavalue

     foreach (ClassX rowitem in bigList)

    //       string Datavalue=rowitem.getfield("f2").getvalue().tostring();    or the working equivalent.

      }

    Many thanks 

    I know the syntax rowitem.f2; etc...   but I am trying to pass a fieldname as a parameter into a reusable function

    Thursday, September 20, 2012 9:31 AM
  • User-474980206 posted

    MVC models are standard class instances. to access "fields" you use the reflection library.

     

    Thursday, September 20, 2012 9:43 AM
  • User369345748 posted

    All the samples I have seen of reflection show how to iterate through a collection of fields & get out the "nth" field, rather than jumping straight to the specific named field to get its properties

    Please advise on syntax or post a link to how to achieve this.

    Thursday, September 20, 2012 9:47 AM
  • User-1280676833 posted

    You should get the model type first, like this:

    string Datavalue=rowitem.GetType().GetField("f2").GetValue().ToString();    

    Hope this helpful
    Regards

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 26, 2012 2:27 AM