How to loop through all the fields in an MVC Model, without naming them in advance?

คำถาม How to loop through all the fields in an MVC Model, without naming them in advance?

  • 22 สิงหาคม 2555 8:08
     
     

    I am building an MVC remote sharepoint app & have written some code to push a ListItemCollection to a JSON Feed, without needing to know the field names.  How can I do the same for a custom model passed as formdata from the view ? Here's a snippet of the JSON code, which shows what I'd like to do

       foreach (ListItem oListItem in IndListItem)

               {

                    int fcount =oListItem.FieldValues.Keys.Count;
                    for (int j = 0; j < fcount; j++)
                    {                     fname= oListItem.FieldValues.Keys.ElementAt(j);
    fval = HttpUtility.HtmlEncode(oListItem.FieldValues[fname].ToString());                 }
                     json += '"' + fname + '"' + ":" + '"' + fval + '"' + ",";
                    }
    I want to acheive something like 

    sp.ListItem oListItem = list.GetItemById(id);

    foreach (xField in formdata)

    {oListItem[xField] = formdata.getField(xField).ToString();}


    Richard




    • แก้ไขโดย Richard Scannell 22 สิงหาคม 2555 8:11
    • ย้ายโดย Bob ShenMicrosoft 3 กันยายน 2555 7:37 (From:Visual C# Language)
    •  

ตอบทั้งหมด