RMD*_*RMD 1 c# asp.net-mvc razor
我是 ASP.NET MVC 的新手。我有RestCallcontroller课。我想将数据从Restcontrollerto传递Home Controller。这是RestController
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 });
}
}
Run Code Online (Sandbox Code Playgroud)
以下是我的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);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的模型课。
public class Result
{
public string loginJsonStringop { get; set; }
public string modelop { get; set; }
public string descriptionop { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我想将 loginJsonString、modelList、descriptionList 的值传递给index()方法Home Controller并在索引视图中查看它。如果您有任何建议,请帮助我。
我们TempData在 MVC 中将数据从一个控制器传递到另一个控制器。你甚至可以参考答案。
在您的第一个控制器中,您可以做一些事情。
TempData["jsonData"] = ANY_OBJECT;
然后在你的家庭控制器中你可以得到它。
var object = TempData["jsonData"];
更新
要记住的临时数据限制
但有一个问题......临时数据将仅在第一次调用控制器时可用。如果您在休息控制器之后重定向到家庭通行证,您将能够在家庭控制器中获取临时数据,但是如果您进行了一些重定向,然后您定向到家庭,并尝试获取临时数据,它将无法正常工作。如果您需要,并认为创建适当的模型并将其传递给家庭控制器是一个很好的解决方案。
更新 您正在尝试使用模型传递数据,然后您可以做一些事情。——
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 });
}
Run Code Online (Sandbox Code Playgroud)
然后你的家庭控制器必须在Index行动中接收这个模型。
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();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3369 次 |
| 最近记录: |