Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I am working on an asp.net mvc web application, that connects to two different DB (I name them dbA & dbB). Currently I need to create a JSON object that contains values from different tables.

The two table are as follow:-

Table Technology from dbA, have the following fields:-

>> dbAID , Tag , db2ID

While table Resource from dbB have:-

>> Db2ID , Name

I have the following Action method:-

public ActionResult AutoComplete(string term)

        {

var tech = dbA.Technology.Where(a=>a.Tag.StartWith(term));//select all technology that have their tags start with passed term

var db2IDList = tech.Select(a=>a.db2ID).ToArray();//create a list of db2ID



var resource = dbB.Resource.Where(db2IdList.Contains(a=>a.dbBID));//get the resource based on the db2IDList



JsonResult j = newJsonResult();

//here I want the json to contain the Technology.Tag + ResoruceNane, where the join is based on the db2Id stored insdie the technology table



        }

So can anyone adivce what is the best way to construct my JSON object?

Thanks

share|improve this question

Anonymous Types: http://msdn.microsoft.com/en-us/library/bb397696.aspx

And the framework provided JsonResult, generated by the Json(object) helper.

e.g. return Json(new { field1 = resource.fieldOne, field2 = resource.fieldTwo })

share|improve this answer
    
but what is resource.fieldOne and resource.fieldTwo ?? – john G Aug 1 '14 at 14:47
    
From your question: var resource = dbB.Resource.Where(db2IdList.Contains(a=>a.dbBID));//get the resource based on the db2IDList – Gareth Saul Aug 1 '14 at 14:52
    
but how the two fields will be linked together ? – john G Aug 1 '14 at 15:00

you can construct a List i think its easier :

List<Object> _return=new List<object>();
_return.Add(Technology.Tag);
_return.Add(ResoruceName);
Return Json(new {Success=true, _return});

//and then cast get these object in your Javascript , it would be an easy to use array

share|improve this answer
    
but will this approach join the two lists, i mean how i will know which tag relates to which resource name ? – john G Aug 1 '14 at 15:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.