简体   繁体   中英

MVC + How can i alert prompt user before Controller action

MVC Newbie here.

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? or is there a good way.

You can probably do with an onClick event handler:

<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.

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. You can store any data you need to pass to the confirmation action in TempData or 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. That's the general idea, you can add more messages or steps as you need.

In the 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. 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.

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.

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.

cheers :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM