Marta
Marta

Reputation: 2947

How to trigger popup confirmation from controller

I have a MVC 3 website and I need to set a warning with confirmation when user is trying to add a record for the same date. User clicks on a button on a view:

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

Here is a code from controller. Button tat triggers this action is at StartWork view.

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");
}

When it is a duplicate I want to trigger some kind of a popup with warning and options to proceed anyway or cancel.

I have tried rendering PartialView but it just rendered new view.

Any help much appreciated!

Upvotes: 0

Views: 1846

Answers (1)

Rory McCrossan
Rory McCrossan

Reputation: 337714

Try using a flag set in ViewData to indicate whether the confirm box should be shown in your view:

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

Then in your view:

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

Upvotes: 2

Related Questions