【发布时间】:2015-11-05 14:55:02
【问题描述】:
我有一个 mvc Web 应用程序,用户在其中向服务器发送请求。在执行方法 DoSomething(int x) 时,会发生验证。所以我需要向用户发送回复请求确认。如果用户单击是,我应该继续执行 DoSomething(int x)。如果用户响应否,我将停止让用户进行一些更改并再次重新提交。 我试图在下面的代码中模仿我的场景:这个想法是让你理解这个问题。 现在的问题是:有没有办法使用线程? 提示:如果这是一个 windows 窗体,Showdialog 会很容易地处理它。
public void DoSomething(int x)
{
// do other thing here
//...
if (x > 0)
{
//Notify user for confirmation
EventNotification.ModalBox("X is out of the Decision", "Do you want to proceed?");
//httprequest will fire based on user response
//###PAUSE HERE ####
//wait for user response
}
//###RESUME HERE ####
//Continu here if confirmation is YES
x = x + 23;
//save to database
EventNotification.CloseBox("good");
}
public class EventNotification
{
public event System.EventHandler CloseEvent;
public event System.EventHandler ModalBoxEvent;
public void ModalBox(string text, string YesorNoConfirmation)
{//raise event to open modal box
MyMsgBoxText = text;
MyMsgBoxTitle = YesorNoConfirmation;
if (ModalBoxEvent != null)
{
ModalBoxEvent(this, System.EventArgs.Empty);
}
}
public void CloseBox(string text)
{
if (CloseEvent != null)
{
CloseEvent(this, System.EventArgs.Empty);
}
}
}
【问题讨论】:
-
为什么不在 javascript 中进行验证/确认,然后再提交到服务器?
-
在非常正常的情况下是的。但我正在处理一个糟糕的 web 设计中的特殊应用程序。(最初在 windows 中)。
-
“非常正常的情况。我正在处理一个特殊的应用程序应用程序” 对不起,我不明白你在说什么.
-
我们通过保持所有后端不受影响将 Windows 窗体应用程序转换为 MVC
-
不幸的是,您将不得不触摸后端,在 MVC 中根本没有“暂停”的好方法,因为如果用户在您离开时(或关闭浏览器)会怎样?暂停?如果你使用了一个线程,你将有一个资源坐在那里永远等待。它需要分成两步过程,首先是确认,然后是操作,或者在 javascript 中进行确认。
标签: c# asp.net asp.net-mvc multithreading modal-dialog