Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

Was wondering what the best way is to pull data from an API (in JSON format). I have code in my controller, which calls an API which returns data.

I want to get the data onto my View so i can display it on a page. I have seen the most documented way by using jQuery/AJAX, but i dont really want the API url's to be made public.

I was thinking of passing an object created from the returned data. But in all honesty i am not sure how to do this!

The below code brings back the data for the products, per user. This works fine.

public static List<productDetails> GetUserProducts(string userid)
{
    //api/product/user/<Guid>
    var url = baseUrl + "/product/user/" + userid;

    var syncClient = new WebClient();
    var content = syncClient.DownloadString(url);

    List<productDetails> Products = (List<productDetails>)Newtonsoft.Json.JsonConvert.DeserializeObject(content, typeof(List<productDetails>));

    return Products;
}

And at present i am passing the returned data to the page using ViewBag. This does not work well if there is more than one product. I am passing this in the ActionResult for the view.

var p = GetUserProducts(userGuid);

foreach(var product in p)
{
    ViewBag.pId = product.Id;
    ViewBag.pName = product.FriendlyName;
    ViewBag.pSerial = product.SerialNumber;
    ViewBag.pbatt = product.Location.BatteryCharge + "%";

    ViewBag.devicehistory = "~/Location/History/" + product.Id;
}

Any ideas/examples would be much appreciated.

share|improve this question
    
Why don't you just use Model instead of ViewBag? – cem May 12 '14 at 8:48
    
I have a model set up, i am pretty new to MVC tbh. do you have an example? – thatuxguy May 12 '14 at 8:53
up vote 5 down vote accepted

Hope this can give you some idea on how it actually work

Some example of return as actionresult to view

Controller

public ActionResult something(string userGuid)
{
    var p = GetUserProducts(userGuid);
    return view(p); //you can return as partial view  (return PartialView("your partial view name", p));
}

View

@model IEnumerable<productDetails>


 foreach (var item in Model)
{
   @Html.DisplayFor(model => item.Id)
   //and so on
}

JsonResult

Some example of return as json to view

Controller

   [httpPost]
    public JsonResult something(string userGuid)
    {
        var p = GetUserProducts(userGuid);
        return Json(p, JsonRequestBehavior.AllowGet);
    }

call with ajax

$.post( "../something", {userGuid: "foo"}, function( data ) {
  console.log(data)
});
share|improve this answer
    
First part works a treat, just what i needed i think :D Cheers! – thatuxguy May 12 '14 at 9:27
    
what if i had a 2nd -- var p2 = GetUserOtherProducts(userGuid); for example? how would i pass that to the view? would that be using partial views? – thatuxguy May 12 '14 at 9:30
1  
If both is totally different model(should be different model), then you need to create a ViewModel, you can refer this stackoverflow.com/questions/16548376/… for more info – Se0ng11 May 12 '14 at 9:33
    
cheers i will take a look – thatuxguy May 12 '14 at 10:01
    
used this one too -- stackoverflow.com/questions/6937156/… Thanks for your help! :D – thatuxguy May 12 '14 at 10:07

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.