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 likesp.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)
ตอบทั้งหมด
-
22 สิงหาคม 2555 8:14The MVC forums http://forums.asp.net/1146.aspx is the better place for such questions
With Thanks and Regards
Sambath Raj.C
click "Proposed As Answer by" if this post solves your problem or "Vote As Helpful" if a post has been useful to you
Happy Programming! -
23 สิงหาคม 2555 5:30
Hi Richard,
You are more likely to get more efficient responses to ASP.NET issues at http://forums.asp.net where you can contact ASP.NET experts.
Bob Shen [MSFT]
MSDN Community Support | Feedback to us