asp.net mvc - Using a JSONResult in a C# function -


i have function returns jsonresult in below way.

var attachments = (from in ar.attachments    select new { id = a.id, filename = a.filename }).toarray();  var result = new             {                 comments = "some string",                 attachments = attachments             };          return this.json(result); 

i need use result in class need access "comments" , "attachments". here attachments string array , comments string. please let me know how can this.

you create viewmodel result , reuse class. viewmodel is, poco or dto. idea gives different way "look" @ data, nothing special really.

so end 3 parts.

the data method:

public commentsviewmodel getviewmodel() {     var attachments =          (from in ar.attachments          select new { id = a.id, filename = a.filename }).toarray();      var result = new commentsviewmodel             {                 comments = "some string",                 attachments = attachments             };      return result; } 

your controller method:

public jsonresult get() {     return this.json(getviewmodel()); } 

and other method call getviewmodel() directly. separate out bit you.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -