Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create the confirm box in mvc controller?

I need to create the confirm box in mvc controller?. Using this 'yes' or 'no' value I need to perform the action in my controller. How we do that?

Sample code:

    public ActionResult ActionName(passing value)
        {
             // some code 
             message box here
               if (true)
                     { true code}
              else { else code}
       }
like image 699
user279stack1 Avatar asked Mar 21 '13 12:03

user279stack1


People also ask

How do you make a confirm box?

You can create a JavaScript confirmation box that offers yes and no options by using the confirm() method. The confirm() method will display a dialog box with a custom message that you can specify as its argument.

Which function is used for confirmation by dialog box?

The confirm() method displays a dialog box with a message, an OK button, and a Cancel button. The confirm() method returns true if the user clicked "OK", otherwise false .

How can show alert message in button click in MVC?

FormMethod – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST. The Form consists of two elements i.e. a TextBox and a Submit Button. The ViewBag object named Message is checked for NULL and if it is not NULL then the value of the object is displayed using JavaScript Alert MessageBox.


1 Answers

You can do this with ActionLink

@Html.ActionLink(
    "Delete", 
    "DeleteAction", 
    "Product", 
    new { confirm = true, other_parameter = "some_more_parameter" }, 
    new { onclick = "return confirm('Do you really want to delete this product?')" })

If user confirm, then link parameter will pass to the controller action method.

public ActionResult DeleteAction(bool confirm, string other_parameter)
{
    // if user confirm to delete then this action will fire
    // and you can pass true value. If not, then it is already not confirmed.

    return View();
}

Update

You can not show message box in controller side. But you can do this like following

public ActionResult ActionName(passing value)
{
     // some code 
     message box here
     if (true){ ViewBag.Status = true }
     else { ViewBag.Status = false}

     return View();
}

And view

<script type="text/javascript">
function() {
    var status = '@ViewBag.Status';
    if (status) {
        alert("success");
    } else {
        alert("error");
    }
}
</script>

But these all codes are not elegant way. This is solution of your scenerio.

like image 65
AliRıza Adıyahşi Avatar answered Sep 19 '22 02:09

AliRıza Adıyahşi