7

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.

2
  • Why don't you just use Model instead of ViewBag?
    – cem
    Commented May 12, 2014 at 8:48
  • I have a model set up, i am pretty new to MVC tbh. do you have an example?
    – thatuxguy
    Commented May 12, 2014 at 8:53

1 Answer 1

6

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)
});
4
  • 1
    First part works a treat, just what i needed i think :D Cheers!
    – thatuxguy
    Commented May 12, 2014 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
    Commented May 12, 2014 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
    Commented May 12, 2014 at 9:33
  • used this one too -- stackoverflow.com/questions/6937156/… Thanks for your help! :D
    – thatuxguy
    Commented May 12, 2014 at 10:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.