![](/img/trans.png)
[英]how to create an alert prompt in the controller before redirecting to a new view?
[英]MVC + How can i alert prompt user before Controller action
MVC新手在这里。
我想在控制器操作之前得到用户确认(更新记录)
我的代码:
[HttpPost]
public ActionResult JobHandlerUpdate(int jobScheduleId, JobHandlerList jobHandlerList)
{
var updateJobHander = new MainJobHandler();
var item = updateJobHander.GetById(jobScheduleId);
if (ModelState.IsValid)
{
List<string> days = jobHandlerList.JobProcessDayOfWeek.Split(',').ToList();
updateJobHander.Update(item, days);
if(jobHandlerList.MaxInstances == 0)
{
// here I need to prompt user if maxInstances entered is Zero-
Job will be disabled want to processs (Y/N) if yes update
else do nothing or redirect to edit screen
}
return RedirectToAction("JobHandler");
}
return View(item);
}
我需要使用JavaScript警报吗? 还是有一个好方法。
您可能可以使用onClick事件处理程序:
<input type="submit" onclick="return confirm('Are you sure you wish to submit?');" />
您只能执行客户端提示,因为您的Controller代码在服务器上执行,因此客户端当然不能访问。
如果您不想(或不能)使用JavaScript,则将其分为两步:在一个动作中您进行验证,然后重定向到执行确认的动作。 您可以将传递给确认操作所需的任何数据存储在TempData或Session中。
[HttpPost]
public ActionResult JobHandlerUpdate(int jobScheduleId, JobHandlerList jobHandlerList)
{
var updateJobHander = new MainJobHandler();
var item = updateJobHander.GetById(jobScheduleId);
if (ModelState.IsValid)
{
List<string> days = jobHandlerList.JobProcessDayOfWeek.Split(',').ToList();
updateJobHander.Update(item, days);
if(jobHandlerList.MaxInstances == 0)
{
// Redirect to confirmation View
return View("JobUpdateConfirmation");
}
return RedirectToAction("JobHandler");
}
return View(item);
}
[HttpPost]
public ActionResult JobUpdateConfirmation()
{
// Code to update Job here
// Notify success, eg. view with a message.
return RedirectToAction("JobHandlerUpdateSuccess");
}
您将需要一个视图(JobUpdateConfirmation),该表单带有一个要求确认的表格,然后回发到JobUpdateConfirmation。 这是一般想法,您可以根据需要添加更多消息或步骤。
在ASPX中:
<%= Html.ActionLink("link text", "ActionName", "ControllerName", new { actionMethodVariable = item.ID }, new { @onclick = "return confirm_dialog();" })%>
<script type="text/javascript">
function confirm_dialog() {
if (confirm("dialog text") == true)
return true;
else
return false;
}
</script>
//controller
public ActionResult ActionName(int laugh)
{
if (ModelState.IsValid)
{
//bla bla bla
}
return something;
}
我认为这更多的是在UI流程设计上,而不是在控制器设计上。 您是否可以在提交给第一个控制器之前就提示用户有关待处理的更改?
我认为javascript /客户端确认在这里很理想。
或者您也可以按照CGK建议的另一种方式进行操作,并在页面提交后重定向到第二个控制器,也许可以查看它,以便在实际更新记录之前获得用户确认,或者如果他选择不这样做,则只需重定向回上一页。
我打算在我认为其他答案试图说的内容上添加评论,但是由于我不能100%肯定,所以我认为我只是在这里写下了自己的想法。
欢呼:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.