1

I am new to ASP.NET MVC. 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; }
    }

I want to pass value of loginJsonString, modelList,descriptionList to index() method in Home Controller and view that in index view. If you have any suggestions please help me.

RMD
  • 311
  • 1
  • 7
  • 22
  • Possible duplicate of [Passing data between different controller action methods](https://stackoverflow.com/questions/15385442/passing-data-between-different-controller-action-methods) – Deepak Sharma Mar 08 '18 at 06:12

1 Answers1

7

We have TempData in MVC to pass the data from one controller to another. you can even refer the Answer.

In your first controller you can do something.

TempData["jsonData"] = ANY_OBJECT;

And then in you home controller you can get it.

var object = TempData["jsonData"];


Update


Temp Data Limitation to keep in mind

but there is a catch.. temp data will be available only first call to controller. if you redirect to home pass after your rest controller you will be able to get temp data in home controller, but if you did some redirection and then you directed to home, and tried to get temp data it will not work. if you need that, and think creating proper model, and passing it to home controller is a good solution.

UPDATE you are trying to pass data using model then you can do something. --

public async Task<ActionResult> RunAsync(string a, string b)
{
  ...
  ...
  ...
  Result obj = new Result();
  obj.loginJsonStringop = "VALUE_OF_FIELD";
  ...
  ...
  ...
  return RedirectToActtion("Index", "HomeController",new { resultop =result });
}

and then you home controller must recieve this model in Index action.

public class HomeController : Controller
{       
    public ActionResult Index(Result resultop)
    {
        // do whatever you want to do with your "resultop" instance of type "Result"
        var value = resultop.loginJsonStringop;    // "VALUE_OF_FIELD"

        return View();
    }
}
Deepak Sharma
  • 4,124
  • 1
  • 14
  • 31
  • Thanks.Could you please explain with an example. – RMD Mar 08 '18 at 08:55
  • you can check these two link. https://www.aspsnippets.com/Articles/ASPNet-MVC-TempData-Tutorial-with-example.aspx and https://www.codeproject.com/Articles/786603/Using-TempData-in-ASP-NET-MVC first one we set temp data then redirect to second controller and access the temp data directly into view. While in second example set the data and then get the data into controller and that you alter if required and then pass to view. its upto you whatever you want to do with that data. – Deepak Sharma Mar 08 '18 at 09:12
  • I created a result class and assigned json value toa field of that class.Then tried to pass object of result class to the Homecontroller as 'Result result = TempData["Result"] as Result;' But I am still unable to get values. – RMD Mar 08 '18 at 10:05
  • how you are redirecting from 'RestController' to 'HomeController'? can you create donetfiddle and share ?? – Deepak Sharma Mar 08 '18 at 10:25
  • return RedirectToAction("Index", "HomeController",new { resultop =result }); – RMD Mar 08 '18 at 10:32
  • see here you are passing model of result, so why you are focusing on tempData. you can pass all your requirement using this model from rest to home. but make it sure your `Index` action in home controller just accept this model result. one thing to notice, you are passing `result` object with name of `resultop` so make it sure you Home/Index action has parameter of type `Result` with name `resultop` – Deepak Sharma Mar 08 '18 at 10:38
  • public ActionResult Index(Result resultop) { RestCallController restcallController = new RestCallController(); restcallController.RunAsync(a,b); resultop.loginJsonStringop = restcallController.loginJsonString; return View(resultop); } I edited the home controller as above. But still cannot get values for 'loginJsonStringop' . 'Result' is my model and 'loginJsonStringop' is the field in the model. Can you please help me? – RMD Mar 09 '18 at 03:31