以下是我遇到的问题 - 我创建了一个包含开始和停止按钮的视图。截至目前,当用户单击开始按钮时,我将布尔变量“start”设置为true并更新一些数据库记录。我需要做的是,一旦记录更新,返回并呈现相同的视图,然后进入一个方法,继续更新数据库记录,直到用户点击停止按钮 - 这将设置“开始”变量false,然后将停止代码更新数据库。
我尝试使用OnResultsExecuted Action过滤器,该过滤器在返回视图后触发,但在呈现视图之前不会触发 - 这使得用户无法在循环内单击停止按钮。
非常感谢任何建议!代码片段在
之下查看:
<div class="col-md-offset-1">
<div class="form-group" id="stress">
<input type="submit" value="START" name="Command" title="START" class="btn1 btn-default" id="start" />
</div>
</div>
<div class="col-md-offset-1">
<div class="form-group" id="stress">
<input type="submit" value="STOP" name="Command" title="STOP" class="btn1 btn-default" id="stop" />
</div>
</div>
控制器:
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(在返回并呈现View后需要执行的代码)
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");
}
}
}
}