首页 > 解决方案 > 回发后从控制器返回列表到视图

问题描述

控制器

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 的新手。

标签: asp.net-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>
       }
    }
  }

推荐阅读