【问题标题】:AutoSwitch Two Views in Asp.Net MVC在 Asp.Net MVC 中自动切换两个视图
【发布时间】:2018-08-15 14:21:17
【问题描述】:

是否可以以固定间隔(即 5 秒或任何时间)显示两个视图?所以我想从同一个视图中显示两个视图。如何通过路由配置或控制器中的操作方法使其成为可能。我想以固定的时间间隔显示下面的动作视图。

public ActionResult FlightBoardingDisplay()
{
    return View(db.tblFlightSchedules.OrderBy(m => m.Time).Where(m => m.Origin == "KATHMANDU").ToList());
}

public ActionResult FlightStatusDisplay()
{

    return View(db.tblFlightSchedules.OrderBy(m => m.Time).Where(m => m.Origin == "KATHMANDU").Where(m => m.FSId == 4).Where(m=>m.FSId ==1).Where(m=>m.FSId==3).ToList());
}

【问题讨论】:

  • 当然可以。只需编写将在固定时间间隔内请求适当操作的 JS。

标签: c# asp.net asp.net-mvc razor


【解决方案1】:

您可以执行此操作,例如在一段时间后调用操作方法。FlightBoardingDisplay 视图类型必须为Partial View

<script>
  window.onload=function(){
setTimeout(function(){ callFlightStatusDisplay(); callFlightBoardingDisplay(); }, 
 3000);
 }


   function callFlightStatusDisplay()
   {
  $.ajax({
   type: "GET",
  url: '@Url.Action("FlightStatusDisplay", "ControllerName")',
  contentType: "application/json; charset=utf-8",

  dataType: "json",
  success: function(data) { alert('Success'); 
     $('#DIVIDTOINSERTRESPONSE').html(data);
  },
  error: function() { alert('A error'); }
    });
 }

  function callFlightBoardingDisplay()
  {
  $.ajax({
   type: "GET",
   url: '@Url.Action("FlightBoardingDisplay", "ControllerName")',
   contentType: "application/json; charset=utf-8",
   dataType: "json",
   success: function(data) { alert('Success'); 
     $('#DIVIDTOINSERTRESPONSE').html(data);
   },
   error: function() { alert('A error'); }
     });
   }
 </script>

控制器

       [HttpGet]
       public ActionResult FlightBoardingDisplay()
      {
        string date = String.Format("{0:D}", DateTime.Now.Date);
        ViewBag.Date = date;

        return PartialView(db.tblFlightSchedules.OrderBy(m => m.Time).Where(m => m.Origin == 
        "KATHMANDU").ToList());
   }

[HttpGet]
public ActionResult FlightStatusDisplay()
{
    string date = String.Format("{0:D}", DateTime.Now.Date);
    ViewBag.Date = date;

    return PartialView(db.tblFlightSchedules.OrderBy(m => m.Time).Where(m => m.Origin == "KATHMANDU").Where(m => m.FSId == 4).Where(m=>m.FSId ==1).Where(m=>m.FSId==3).ToList());
}

【讨论】:

  • 如我所见,您的 JS 在 3 秒内会调用 callActionmethodwithpara(2); 并立即调用 callActionmethodwithoutpara();,因此您页面上的内容会闪烁
  • 不,我的意思是这取决于他如何称呼通话视图
  • 我认为,您的答案必须是“完全有效”,“有一些保留意见”(如 cmets // implement this as needed),但是“不工作”或“工作不如预期”回答了一个完全不可接受的问题。因此,如果您发布答案,请找时间修复它。
  • 好的,如果用户不同意当前的解决方案,我会做得更好
  • 告诉我你有没有用局部视图替换你的视图?
【解决方案2】:

我只需插入即可找到解决方案

    <meta http-equiv="refresh" content="5;url=http://192.168.5.34:8084/FlightInfo/FlightBoardingDisplay" />

在头部的两个视图中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-21
    • 1970-01-01
    相关资源
    最近更新 更多