最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - MVC + How can i alert prompt user before Controller action - Stack Overflow

programmeradmin5浏览0评论

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.

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.

Share Improve this question asked Dec 2, 2010 at 0:21 SreedharSreedhar 30k39 gold badges127 silver badges197 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

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 ments 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 :)

发布评论

评论列表(0)

  1. 暂无评论