简体   繁体   English

MVC +如何在控制器操作之前提醒提示用户

[英]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.

相关问题 重定向到新视图之前,如何在控制器中创建警报提示? - how to create an alert prompt in the controller before redirecting to a new view? 如何从MVC控制器动作调用asyc方法? - How can I call an asyc method from MVC controller action? 在 ASP.net MVC 中,我可以在完成 controller 操作之前处理我的 cshtml 逻辑吗? - In ASP.net MVC Can I process through my cshtml logic before finishing the controller action? 取消操作并显示来自 MVC 控制器的警报 - Cancel action and show alert from MVC controller 我如何获取MVC4 Url.Action方法在控制器之前包含哈希 - How do I get MVC4 Url.Action method to include a hash before the controller 我如何提示用户登录 - How can i Prompt user log in 如何使用甜蜜警报发布到控制器动作结果方法? MVC5 C# - How to post to an controller action result method using sweet alert? mvc5 c# 在 ASP.NET MVC 中执行控制器后如何将数据发送到操作过滤器? - How Can I Send Data to the Action Filter after Execution of the controller in ASP.NET MVC? 我如何使用formdata将字典(键:值)对数据分配给MVC中的控制器操作 - How i can assign dictionary(key:value) pair data using formdata to controller action in MVC 当我在asp.net mvc控制器操作中验证失败时,如何保留我的URL - how can i keep my url when my validation fail in asp.net mvc controller action
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM