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.