asp.net-mvc - 回发后从控制器返回列表到视图
问题描述
控制器
public ActionResult ImportListByVesselVoyage()
{
return View();
}
[HttpPost]
public ActionResult ImportListByVesselVoyage(WebTrackParam param)
{
client = new HttpClient();
List<ImportListByVesselVoyageGetILBV> appMenu = new List<ImportListByVesselVoyageGetILBV>();
client.BaseAddress = new Uri(url);
MediaTypeWithQualityHeaderValue contentType = new MediaTypeWithQualityHeaderValue("application/json");
client.DefaultRequestHeaders.Accept.Add(contentType);
try
{
HttpResponseMessage response = client.GetAsync(string.Format("/Services/WebTrack.svc/ImportListByVesselVoyageGetILBVJSONData/{0}/{1}/{2}/{3}/{4}/{5}/{6}/{7}/{8}/{9}/{10}", "VR", "2018", param.reg, " ", " ", "A", "A", "S,Y,D", "F,L,E,T", "ITTEST", "2")).Result;
if (response.IsSuccessStatusCode)
{
var stringData = response.Content.ReadAsStringAsync().Result;
JObject result = JObject.Parse(stringData);
var appMenuarray = result["ImportListByVesselVoyageGetILBVResult"].Value<JArray>();
appMenu = appMenuarray.ToObject<List<ImportListByVesselVoyageGetILBV>>();
ViewBag.ILBV = appMenu;
}
return View();
}
catch (Exception ex)
{
return PartialView("ImportListByVesselVoyage", appMenu);
}
finally
{
client.Dispose();
client = null;
}
}
看法
@model List<WebTrack.Models.ImportListByVesselVoyageGetILBV>
@using (Html.BeginForm("ImportListByVesselVoyage", "Containerized", FormMethod.Post))
{
<div class="box-body">
<div class="form-group">
<div class="col-sm-6">
@Html.TextBoxFor(model => model.reg)
</div>
</div>
</div>
<div class="box-footer">
<button type="submit" class="btn btn-info pull-right"><i class="fa fa-search"></i>Search</button>
</div>
<div id="result">
<div class="col-md-12">
<div class="box">
<div class="box-body no-padding">
<table class="table table-striped">
<tbody>
<tr>
<th>Container No.</th>
<th>Operator</th>
<th>Status</th>
<th>Size</th>
<th>Commodity</th>
<th>Destination</th>
<th>Location</th>
<th>Discharged</th>
<th>Delivered</th>
</tr>
@foreach (var item in ViewBag.ILBV)
{
<tr>
<td>@item.ContainerNo</td>
<td>@item.Operator</td>
<td>@item.Status</td>
<td>@item.Size</td>
<td>@item.Commodity</td>
<td>@item.Destination</td>
<td>@item.Location</td>
<td>@item.Discharged</td>
<td>@item.Delivered</td>
</tr>
}
</tbody>
</table>
</div>
<div class="box-footer clearfix">
<ul class="pagination pagination-sm no-margin pull-right"></ul>
</div>
</div>
</div>
</div>
}
我将从 @Html.TextBox() 获取“reg”并将其传递给[HttpPost] public ActionResult ImportListByVesselVoyage(WebTrackParam param)。我想知道如何将结果 appMenu 返回到我的视图。而且我尝试使用 ViewBag.ILBV 但由于它位于 HttpPost 上,因此在获取期间会导致 null 错误。
谢谢,希望你能理解我的担心。我是 MVC 的新手。
解决方案
而且我尝试使用 ViewBag.ILBV 但由于它位于 HttpPost 上,因此在获取期间会导致 null 错误。
所以写你View
的如下:
@model List<WebTrack.Models.ImportListByVesselVoyageGetILBV>
@using (Html.BeginForm("ImportListByVesselVoyage", "Containerized", FormMethod.Post))
{
<div class="box-body">
<div class="form-group">
<div class="col-sm-6">
@Html.TextBoxFor(model => model.reg)
</div>
</div>
</div>
<div class="box-footer">
<button type="submit" class="btn btn-info pull-right"><i class="fa fa-search"></i>Search</button>
</div>
<div id="result">
@{
if(ViewBag.ILBV != null)
{
<div class="col-md-12">
<div class="box">
<div class="box-body no-padding">
<table class="table table-striped">
<tbody>
<tr>
<th>Container No.</th>
<th>Operator</th>
<th>Status</th>
<th>Size</th>
<th>Commodity</th>
<th>Destination</th>
<th>Location</th>
<th>Discharged</th>
<th>Delivered</th>
</tr>
@foreach (var item in ViewBag.ILBV)
{
<tr>
<td>@item.ContainerNo</td>
<td>@item.Operator</td>
<td>@item.Status</td>
<td>@item.Size</td>
<td>@item.Commodity</td>
<td>@item.Destination</td>
<td>@item.Location</td>
<td>@item.Discharged</td>
<td>@item.Delivered</td>
</tr>
}
</tbody>
</table>
</div>
<div class="box-footer clearfix">
<ul class="pagination pagination-sm no-margin pull-right"></ul>
</div>
</div>
</div>
}
}
}
推荐阅读
- linux - 使用 pid 查找 TCP 连接
- sql - 将 SQL Server 查询转换为 Postgres 查询
- peoplesoft - 使用 Peoplesoft 数据元素生成 HTML 格式的电子邮件
- python - 如何从字符串键定义python常量
- continuous-integration - Artifactory 旧工件清理 (AQL) 并保留最新 3 个版本
- mongodb - Mongo:在 v4.4 异常中删除集合
- angular - 如何在离子 5 中添加选择所有选项到离子选择
- html - 嵌套在 JSON 响应中的未知格式的 HTML
- python - 尽管使用线程,Python Multi camera FPS 还是很慢
- javascript - 基于数据库数据构建嵌套数组