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
- Edited by Richard Scannell Wednesday, September 19, 2012 2:44 PM
- Moved by Bob ShenMicrosoft Contingent Staff Monday, September 24, 2012 6:29 AM (From:Visual C# Language)
All Replies
-
Thursday, September 20, 2012 6:36 AM
Hi Richard,
You can consider posting it at the following more appropriate forum for more efficient responses. Thanks!
http://forums.asp.net/1146.aspxBob Shen [MSFT]
MSDN Community Support | Feedback to us
- Proposed As Answer by Mr. WhartyMicrosoft Community Contributor, Moderator Friday, September 28, 2012 5:43 AM
- Marked As Answer by Mr. WhartyMicrosoft Community Contributor, Moderator Friday, September 28, 2012 5:43 AM
-
Monday, September 24, 2012 8:29 AM
Hi Bob
As suggested I reposted the query to
http://forums.asp.net/p/1844897/5152295.aspx/1?Re+getting+a+field+value+from+an+MVC+model
Thank you for tidying up, & I apologise for posting "off topic". Although sometimes its confusing to know how to best categorise a syntax question : .NET , MVC , C# or ASP. As questions often cover several areas, & just which is the most relevant one is difficult to decide.
Any guidance on the appropriate forums for each type of question
Thanks
Richard
Richard

