如何在ASP.NET MVC 5中返回和呈现视图后执行代码
这是我遇到的问题-我创建了一个具有开始和停止按钮的视图。现在,当用户单击开始按钮时,我将布尔变量start设置为true,并更新一些数据库记录。我需要做的是,一旦更新记录,返回并呈现相同的视图,然后进入一个方法,该方法将继续更新数据库记录,直到用户单击停止按钮-该按钮将启动变量设置为false,然后停止代码更新数据库 我尝试过使用OnResultExecuted操作过滤器,该过滤器在返回视图时触发,但在呈现视图之前不会触发,这使得用户无法在循环中单击停止按钮 非常感谢您的任何建议!下面是代码片段 视图: 控制器:如何在ASP.NET MVC 5中返回和呈现视图后执行代码,asp.net,asp.net-mvc-5,action-filter,Asp.net,Asp.net Mvc 5,Action Filter,这是我遇到的问题-我创建了一个具有开始和停止按钮的视图。现在,当用户单击开始按钮时,我将布尔变量start设置为true,并更新一些数据库记录。我需要做的是,一旦更新记录,返回并呈现相同的视图,然后进入一个方法,该方法将继续更新数据库记录,直到用户单击停止按钮-该按钮将启动变量设置为false,然后停止代码更新数据库 我尝试过使用OnResultExecuted操作过滤器,该过滤器在返回视图时触发,但在呈现视图之前不会触发,这使得用户无法在循环中单击停止按钮 非常感谢您的任何建议!下面是代码片段
using V1.ActionFilters;
using V1.Models;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
namespace V1.Controllers
{
public class StressController : Controller
{
private Entities db = new Entities();
//sets on/off variable
public bool start;
//GET: Node/StressTest
//[Authorize]
public ActionResult StressTest()
{
var data = (from item in db.DURR_NODES
select item);
return View(data.ToList());
}
//POST: Node/StressTest
[HttpPost]
[CustomResultFilter]
public ActionResult StressTest(List<NODES> nodes, string Command)
{
//gets count for loops
int nodeCount = (from r in db.DURR_NODES
select r.NODE).Count();
//if the start button is pushed-- kicks off the initial update
//does this because the while loop relies on picking up the REQ_COMPLETE bit,
//which is only turned on when updated success flag turned on (REQ_COMPLETE)
if (Command == "START")
{
start = true;
for (int i = 1; i <= nodeCount; i++)
{
NODES Existed_Node = db.NODES.Find(i);
Existed_Node.NODE = i;
Existed_Node.X_POSITION = newX(i);
Existed_Node.Y_POSITION = newY(i);
Existed_Node.Z_ROTATE = newZ(i);
//newx,y,z methods just calculate updated values for database
Existed_Node.REQUEST_BIT = true;
}
db.SaveChanges();
}
else
{
start = false;
for (int i = 1; i <= nodeCount; i++)
{
NODES Existed_Node = db.ODES.Find(i);
Existed_Node.REQUEST_BIT = false;
}
db.SaveChanges();
}
return View();
}
返回并呈现视图后需要执行的当前ActionFilter代码
using V1.Controllers;
using V1.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
namespace OMSV1.ActionFilters
{
public class CustomResultFilter : ActionFilterAttribute
{
private Entities db = new Entities();
public override void OnResultExecuted(ResultExecutedContext resultExecutedContext)
{
StressController stressCont = new StressController();
bool start = stressCont.start;
int nodeCount = (from r in db.NODES
select r.NODE).Count();
//While start- essentially means until user clicks stop and turns start to false
while (start)
{
//checks for an internal error executing the updates
bool faultExist = false;
for (int j = 1; j <= nodeCount; j++)
{
string fault = (from r in db.NODES
where r.NODE == j
select r.FAULT).SingleOrDefault();
int faultCode = Convert.ToInt32(fault);
if (faultCode != 0)
{
faultExist = true;
}
if (faultExist)
break;
}
//if there is no error executing the updates, continue to update records
if (!faultExist)
{
for (int i = 1; i <= nodeCount; i++)
{
bool RequestComplete = (from r in db.NODES
where r.NODE == i
select r.REQ_COMPLETE).SingleOrDefault();
string fault = (from r in db.NODES
where r.NODE == i
select r.FAULT).SingleOrDefault();
int faultCode = Convert.ToInt32(fault);
bool reqBit = (from r in db.NODES
where r.NODE == i
select r.REQUEST_BIT).SingleOrDefault();
//checks if the request is complete, or if there is no current request or error
//if not then continue with the update
if (RequestComplete || (faultCode == 0 && reqBit == false))
{
NODES Existed_node = db.NODES.Find(i);
Existed_node.NODE = i;
Existed_node.X_POSITION = stressCont.newX(i);
Existed_node.Y_POSITION = stressCont.newY(i);
Existed_node.Z_ROTATE = stressCont.newZ(i);
//same methods that calculate the update to the database
Existed_node.REQUEST_BIT = true;
db.SaveChanges();
}
else
{
//Pass over
//do not need an else statement YET.
}
}
}
else
{
//is there is an internal error turn start off and stop all updated for all records
start = false;
db.Database.ExecuteSqlCommand("UPDATE NODES SET REQUEST_BIT = 0");
}
start = stressCont.start;
}
if (start == false)
{
//if stop button has been clicked and start is now false
//stop all updates for all records
db.Database.ExecuteSqlCommand("UPDATE NODES SET REQUEST_BIT = 0");
}
}
}
}/您是否考虑过这个问题?是否意味着在单击按钮时使用Ajax调用函数,而不是返回一个视图??是的,我是说,在等待某种外部动作发生时,将自己置身于MVC流水线之外。但我曾考虑过只使用AJAX,但在更改所有代码之前,我想确保没有遗漏任何明显的内容。我对AJAX不太熟悉,所以我希望有另一种方法,但这似乎是我希望在SignalR中找到的最好的解决方案。这在这里可能不合适,也可能不合适,但这是一项非常方便的技术。我建议看一些微软的YouTube视频来演示这一点。这是一种从web服务器调用客户端功能的好方法,因此,当web服务器必须执行一些长时间运行的过程,然后通知客户端时,它非常适合。
using V1.Controllers;
using V1.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
namespace OMSV1.ActionFilters
{
public class CustomResultFilter : ActionFilterAttribute
{
private Entities db = new Entities();
public override void OnResultExecuted(ResultExecutedContext resultExecutedContext)
{
StressController stressCont = new StressController();
bool start = stressCont.start;
int nodeCount = (from r in db.NODES
select r.NODE).Count();
//While start- essentially means until user clicks stop and turns start to false
while (start)
{
//checks for an internal error executing the updates
bool faultExist = false;
for (int j = 1; j <= nodeCount; j++)
{
string fault = (from r in db.NODES
where r.NODE == j
select r.FAULT).SingleOrDefault();
int faultCode = Convert.ToInt32(fault);
if (faultCode != 0)
{
faultExist = true;
}
if (faultExist)
break;
}
//if there is no error executing the updates, continue to update records
if (!faultExist)
{
for (int i = 1; i <= nodeCount; i++)
{
bool RequestComplete = (from r in db.NODES
where r.NODE == i
select r.REQ_COMPLETE).SingleOrDefault();
string fault = (from r in db.NODES
where r.NODE == i
select r.FAULT).SingleOrDefault();
int faultCode = Convert.ToInt32(fault);
bool reqBit = (from r in db.NODES
where r.NODE == i
select r.REQUEST_BIT).SingleOrDefault();
//checks if the request is complete, or if there is no current request or error
//if not then continue with the update
if (RequestComplete || (faultCode == 0 && reqBit == false))
{
NODES Existed_node = db.NODES.Find(i);
Existed_node.NODE = i;
Existed_node.X_POSITION = stressCont.newX(i);
Existed_node.Y_POSITION = stressCont.newY(i);
Existed_node.Z_ROTATE = stressCont.newZ(i);
//same methods that calculate the update to the database
Existed_node.REQUEST_BIT = true;
db.SaveChanges();
}
else
{
//Pass over
//do not need an else statement YET.
}
}
}
else
{
//is there is an internal error turn start off and stop all updated for all records
start = false;
db.Database.ExecuteSqlCommand("UPDATE NODES SET REQUEST_BIT = 0");
}
start = stressCont.start;
}
if (start == false)
{
//if stop button has been clicked and start is now false
//stop all updates for all records
db.Database.ExecuteSqlCommand("UPDATE NODES SET REQUEST_BIT = 0");
}
}
}