[RESOLVED]Ajax.BeginForm with delete and edit button?

How to handle delete and edit button on same partial view which has one Ajax.Begin form?

I could use multiple submit button option but i was looking to implement Confirmation dialog on delete button.

How to find a work around for this?

Use one submit button for edit and post the form back to controller for edit action. Clicking on Enter from keyboard will submit the for which is the edit button. For delete use a normal button and call a javascript method. You can use browsers default confirm
or jquery modal pop up box.

<input type="button" value="Delete" onclick="Delete('12')">
<script>
function Delete(id)
{
   var ok=confirm("Do you wan to delete :" + id)
   if (ok)
   {
      //call the delete controller to delete the object, and reload the index page
   }
}
<script>

Hi abhi0410,

For multiple submit button in a form, please refer to:

# Supporting multiple submit buttons on an ASP.NET MVC view

http://blog.maartenballiauw.be/post/2009/11/26/Supporting-multiple-submit-buttons-on-an-ASPNET-MVC-view.aspx

You could refer to that sample to achieve it in Ajax form.

Best Regards

Starain

if following buttons are for edit and to delete from you want delete or edit

<input type="submit" name="cmddeleteoredit" value="Edit" />
<input type="submit" name="cmddeleteoredit" value="Delete" />

here the button text will be submitted to the server. confirm action looks like the below

public ActionResult DeleteOrEdit(int id, string cmddeleteoredit)
{
    ViewBag.cmddeleteoredit = cmddeleteoredit;
    using(var db =new StudentDbContext()){
        var stdnt = db.Student.FirstOrDefault(s => s.Id == id);
        if (stdnt != null)
            return View();//there should be a view with this actionname
        else
        {
            ModelState.AddModelError("", "record not found");
            return View("Detail");
        }

    }
            
}

based on the viewbag value for the mode, you can design the confirm view

@model Model.Student
@using (Ajax.BeginForm("DeleteOrEdit", new AjaxOptions { }))
{
    <div class="form-group">
        @Html.LabelFor(m => m.Name, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @if (ViewBag.cmddeleteoredit=="Edit")            
                @Html.TextBoxFor(m => m.Name, new { @class = "form-control" })            
            else
                @Html.DisplayFor(m => m.Name, new { @class = "form-control" })    
        </div>
    </div>
    <div class="col-md-10">
        @if (ViewBag.cmddeleteoredit == "Edit")
            @Html.TextBoxFor(m => m.Age, new { @class = "form-control" })
        else
        @Html.DisplayFor(m => m.Age, new { @class = "form-control" })
    </div>
    <input id="Submit1" type="submit" value="Save" />
}

Hi cnuonline,

Thanks for your effort.

But i am looking for message box for confirmation on click of delete button before hitting the action method.

<input type="submit" name="cmddeleteoredit" value="Delete" onclick="return confirm('delete?');" />

or

@using (Ajax.BeginForm("DeleteOrEdit", new AjaxOptions {Confirm="delete?" }))
{

Hi cnuonline,

Thanks for the reply. Can i change the button to ‘Yes’ or No?

abhi0410

Thanks for the reply. Can i change the button to ‘Yes’ or No?

Yes

What is the syntax? Any other option for that popup? That popup is plain and look different in IE.

there is little control of the look of the confirm(). many chose to implement their own. there are dozens of dialog box libraries. the main issue of converting from config to a custom dialog, is that the confirm is sync, and all custom are async (that is
there is a callback for clicking yes or no).

Leave a Reply