使用模型类将数据从一个控制器传递到主控制器,并在主页中查看输出。 [英] Pass data from one controller to the home controller using a model class and view the output in home page.
问题描述
我有RestCallcontroller类。我想将数据从Restcontroller传递到Home Controller。这是theRestController
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 });
}
}
以下是我的HomeController。
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>
我想将loginJsonString,modelList,descriptionList的值传递给Home Controller中的index()方法,并在索引视图中查看。
我尝试了什么:
我创建了控制器和模型类如上。但仍然无法获取值。
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.
推荐答案
如果我没记错,我使用此链接中描述的技术在请求之间保留数据(该网站在工作时被阻止,但是URL似乎很熟悉):
在不使用ViewBag的情况下将信息输入布局。 - jgauffin的编码室 [ ^ ]
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[^]
public async Task<ActionResult> RunAsync(string a, string b)
...
restcallController.RunAsync(a,b);
resultop.loginJsonStringop = restcallController.loginJsonString;
async
方法尽快返回因为它命中了第一个 await
。返回的任务
完成后,该方法尚未完成。
因为您还没有等待任务要完成, loginJsonString
字段将不会被设置。
您需要等待
在尝试访问 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.
public async Task<ActionResult> Index(Result resultop)
{
RestCallController restcallController = new RestCallController();
await restcallController.RunAsync(a,b);
resultop.loginJsonStringop = restcallController.loginJsonString;
return View(resultop);
}
这篇关于使用模型类将数据从一个控制器传递到主控制器,并在主页中查看输出。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!