回发后从Controller返回列表以查看

时间:2018-09-27 07:11:36

标签: 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上,因此在获取过程中将导致空错误。

谢谢,希望您了解我的关注。我是MVC的新手。

1 个答案:

答案 0 :(得分:2)

  

我尝试使用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>
       }
    }
  }