【问题标题】:How to trigger popup confirmation from controller如何从控制器触发弹出确认
【发布时间】:2012-04-21 09:38:56
【问题描述】:

我有一个 MVC 3 网站,当用户尝试添加同一日期的记录时,我需要设置警告并进行确认。用户点击视图上的按钮:

<input type="button" onclick="document.location.href = '@Url.Action("StartClock", "Time")'" value="Start Clock" />

这是来自控制器的代码。触发此操作的按钮位于 StartWork 视图中。

public ActionResult StartWork()
{
    return View();
}

public ActionResult StartClock()
{
    if (helper.IsDuplicate(1, DateTime.Now))
    {
        //here I want to trigger a confirmation popup or some warning
    }
    helper.Start(1, DateTime.Now);
    return RedirectToAction("Index");
}

当它重复时,我想触发某种带有警告和选项的弹出窗口,以继续或取消。

我尝试渲染PartialView,但它只是渲染了新视图。

非常感谢任何帮助!

【问题讨论】:

    标签: c# javascript asp.net-mvc asp.net-mvc-3


    【解决方案1】:

    尝试使用ViewData 中设置的标志来指示确认框是否应显示在您的视图中:

    if (helper.IsDuplicate(1, DateTime.Now))
    {
        ViewData["RequireConfirmation"] = true;
    }
    

    那么在你看来:

    @if ((bool)ViewData["RequireConfirmation"]) {
        var answer = confirm ("Are you sure you want to add this record?")
        if (answer) {
            //Confirmed
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多