[英]MVC + How can i alert prompt user before Controller action
MVC Newbie here. MVC新手在这里。
I want to get User confirmation before controller action (update a record) 我想在控制器操作之前得到用户确认(更新记录)
my code: 我的代码:
[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);
}
Do i need to do using javascript alert? 我需要使用JavaScript警报吗? or is there a good way.
还是有一个好方法。
You can probably do with an onClick event handler: 您可能可以使用onClick事件处理程序:
<input type="submit" onclick="return confirm('Are you sure you wish to submit?');" />
You can only do client-side prompts, because your Controller code executes on the server, which of course the client can't access. 您只能执行客户端提示,因为您的Controller代码在服务器上执行,因此客户端当然不能访问。
If you don't want (or can't) use JavaScript, make it a two step process: In one action you validate, then redirect to an action that does the confirmation. 如果您不想(或不能)使用JavaScript,则将其分为两步:在一个动作中您进行验证,然后重定向到执行确认的动作。 You can store any data you need to pass to the confirmation action in TempData or Session.
您可以将传递给确认操作所需的任何数据存储在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");
}
You will need a view (JobUpdateConfirmation) with a form asking for confirmation, and posting back to JobUpdateConfirmation. 您将需要一个视图(JobUpdateConfirmation),该表单带有一个要求确认的表格,然后回发到JobUpdateConfirmation。 That's the general idea, you can add more messages or steps as you need.
这是一般想法,您可以根据需要添加更多消息或步骤。
In the ASPX: 在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;
}
i think this is more on the UI flow design rather than the controller design. 我认为这更多的是在UI流程设计上,而不是在控制器设计上。 Is it possible for you to prompt the the user about the pending changes before even submitting to the first controller?
您是否可以在提交给第一个控制器之前就提示用户有关待处理的更改?
i would think javascript/client side confirmation would be ideal here. 我认为javascript /客户端确认在这里很理想。
or you can do it the other way as CGK suggested and after the page submits, redirect to a second controller, perhaps with view on it to get the user confirmation before actually updating the record or if he choose not to then just redirect back to the previous page. 或者您也可以按照CGK建议的另一种方式进行操作,并在页面提交后重定向到第二个控制器,也许可以查看它,以便在实际更新记录之前获得用户确认,或者如果他选择不这样做,则只需重定向回上一页。
i was planning to add comments to what i thought the other answers were trying to say, but since i can't be 100% positive, i thought i just wrote what i thought here. 我打算在我认为其他答案试图说的内容上添加评论,但是由于我不能100%肯定,所以我认为我只是在这里写下了自己的想法。
cheers :) 欢呼:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.