Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to have an example that can clearly explain me, when or how the first thread is getting detached from a web api call till it receives the data and at which point or how a new thread is being assigned to catch the data and bind back to the browser?

My requirement : I have to display 2 charts in a view, both charts has the web api call to the same controller. But finally only 1 charts data is getting binded in a view.

Please suggest.

Controller action methods :
XML
public async Task<ActionResult> Index()
       {
           return View(await db.Users_Chrt_DataMapping.Where(a=>a.UserName=="Test").ToListAsync());
       }

       // GET: ChartsDashAsync1/Details/5
       public async Task<ActionResult> Details(int? id)
       {
           if (id == null)
           {
               return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
           }
           Users_Chrt_DataMapping users_Chrt_DataMapping = await db.Users_Chrt_DataMapping.FindAsync(id);
           if (users_Chrt_DataMapping == null)
           {
               return HttpNotFound();
           }
           return PartialView(users_Chrt_DataMapping);
       }



Details Partial view calling a web api for the chart data :

$(function (){
      var chtid = '@Model.ChartId';
      alert(chrtdivid);

      $.ajax({
          type: "GET",
          url: "/api/ChartsAPI/" + chtid,
          // data: { chartid: 2 },
          dataType: "json",
          success: function (seriesData) {
              console.log(seriesData);


When i have the action methods as normal unlike async, then i am able to bind only 1 chart in the browser irrespective of how many every charts data.
I have made sure that the div ids are distinct.
Posted
Updated 8-Jul-15 19:13pm
v3
Comments
Sreekanth Mothukuru 8-Jul-15 9:27am    
Post your code so that ppl analyze the issue area along or where you went wrong.
goshan2011 9-Jul-15 1:16am    
thanks for the reply, i have updated my question added the code snippet. Please let me know whts wrong with the code?
Sreekanth Mothukuru 9-Jul-15 3:03am    
I'm not sure where the issue is but..

Instead of calling controller action method twice for each chart control.. why don't you create a view model with mapping properties for both the charts in a single action method. Then no need to call twice for the single partial view.
goshan2011 9-Jul-15 4:12am    
Ok now i made it into single action, i am using like : await db.Users_Chrt_DataMapping.Where(a=>a.UserName=="Test").ToListAsync()

Is there a way by which i can use the await in front a an item of it for ex :
listdata = db.Users_Chrt_DataMapping.ToList();

foreach(var item in listdata)
{
return View(await item);
}

I am getting error at return View(await item); Any idea how to get this?
Sreekanth Mothukuru 9-Jul-15 4:30am    
I'm not sure how to do this but the link below will helps you:

https://msdn.microsoft.com/en-us/magazine/jj991977.aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900