Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have RestCallcontroller class. I want to pass data from Restcontroller to Home Controller. This is theRestController
public class RestCallController : Controller
{
    public  string loginJsonString;
    Result result = new Result();

    // GET: RestCall
    public async Task<ActionResult> RunAsync(string a, string b)
    {

        using (var handler = new HttpClientHandler { UseDefaultCredentials = true })
        using (var client = new HttpClient(handler))
        {

            var byteArray = Encoding.ASCII.GetBytes("username:password");
            client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
            client.BaseAddress = new Uri("XXX");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            HttpResponseMessage response = await client.GetAsync("XXX");

            if (response.IsSuccessStatusCode)
            {
                //Get the response
                 loginJsonString = await response.Content.ReadAsStringAsync();
                result.loginJsonStringop = loginJsonString;
                //Json deserialization
                VehicleResponse vehicleobj = JsonConvert.DeserializeObject<VehicleResponse>(loginJsonString);
                List<string> modelList = new List<string>();
                List<string> descriptionList = new List<string>();

                foreach (Vehicle veh in vehicleobj.Vehicles)
                {
                    var model = veh.Model;
                    var Description = veh.Description;
                    modelList.Add(model);
                    var modellist = modelList;
                    descriptionList.Add(Description);
                }
            }
        }
        return RedirectToAction("Index", "HomeController",new { resultop =result });
    }
}

Following is my HomeController.
public class HomeController : Controller
{
    string a;
    string b;

    // GET: Home
    public ActionResult Index(Result resultop)
    {
        RestCallController restcallController = new RestCallController();
        restcallController.RunAsync(a,b);
        resultop.loginJsonStringop = restcallController.loginJsonString;
        return View(resultop);
    }
}

This is my model class.
public class Result
    {
        public string loginJsonStringop { get; set; }
        public string modelop { get; set; }
        public string descriptionop { get; set; }
    }

Index view.
using IFS_Integration_MVC.Models
@model IFS_Integration_MVC.Models.Result

@{
    ViewBag.Title = "Index";
}

<!DOCTYPE html>
<html>
<head>
    <title>Rest Call</title>
</head>
<body>
    <h2>Index</h2>
    <div><h2>@Model.loginJsonStringop</h2></div>
</body>
</html>

I want to pass value of loginJsonString, modelList,descriptionList to index() method in Home Controller and view that in index view.

What I have tried:

I created controllers and model class as above. But still cannot get values.
Posted
Updated 9-Mar-18 2:43am
Comments
F-ES Sitecore 9-Mar-18 4:15am    
It's not very clear what you're trying to do, but controllers should only be used to processing requests, you don't create one controller from another and run its methods. If you want to pass data between controllers then in the first controller store the data you want to pass in TempData

http://www.tutorialsteacher.com/mvc/tempdata-in-asp.net-mvc

then do return RedirectToAction to redirect the client to the intended action of your intended controller and in that second controller get the data from TempData.

If I recall correctly, I used the technique described at this link to persist data between requests (the site is blocked at work, but the URL seems familiar):

Getting information into the Layout without using ViewBag. – jgauffin's coding den[^]
 
Share this answer
 
Quote:
public async Task<ActionResult> RunAsync(string a, string b)
...
restcallController.RunAsync(a,b);
resultop.loginJsonStringop = restcallController.loginJsonString;

An async method returns as soon as it hits the first await call. The method has not finished until the returned Task has completed.

Since you're not waiting for the task to complete, the loginJsonString field will not be set.

You need to await the returned task before trying to access the loginJsonString field.
C#
public async Task<ActionResult> Index(Result resultop)
{
    RestCallController restcallController = new RestCallController();
    await restcallController.RunAsync(a,b);
    resultop.loginJsonStringop = restcallController.loginJsonString;
    return View(resultop);
}
 
Share this answer
 

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