0

My add/edit code below should redirect back to my index action but it doesn't seem too, it just stays in the same state.

The update function is run (I've checked via debug) then it should return the index view with no dialog and the update data at the moment the dialog just stays and the data isn't updated in the table.

Anyone knows what is wrong? I've also run a capture on it too, nothing I can see there either, just seems to not return the view back:

[HttpPost]
public ActionResult AddEditRecord(tblEquipment Equipment, string cmd)
{
    if (ModelState.IsValid)
    {
        switch (cmd)
        {
            case "Add":
                try
                {
                    db.tblEquipments.Add(Equipment);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                catch { }
                break;
            case "Update":
                try
                {
                    tblEquipment Item = db.tblEquipments.Where(m => m.ID == Equipment.ID).FirstOrDefault();
                    if (Item != null)
                    {
                        Item.AssetNo = Equipment.AssetNo;
                        Item.MachineName = Equipment.MachineName;
                        db.SaveChanges();
                    }
                    return RedirectToAction("Index");
                }
                catch { }
                break;
            }
        }

        if (Request.IsAjaxRequest())
        {
            return PartialView("_AddEdit", Equipment);
        }
        else
        {
            return View("AddEdit", Equipment);
        }
    }
}

EDIT: I put the return at the very beginning of the function (below) and it just ignored it and updated the table!

public ActionResult AddEditRecord(tblEquipment Equipment, string cmd)
{
    return RedirectToAction("Index");
    if (ModelState.IsValid)

EDIT 2: Ok I think it might be an ajax issue.

Looking at this question

My chstml generates the below form, do I need to add a return false to that? CSHTML

@using (Ajax.BeginForm("AddEditRecord", "UserEquipment", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "equipmentDialog" }))

HTML GENERATED

<form id="form0" action="/UserEquipment/AddEditRecord/752" method="post" data-ajax-update="#equipmentDialog" data-ajax-mode="replace" data-ajax-method="POST" data-ajax="true" novalidate="novalidate" jQuery18206614934889497519="43">
Community
  • 1
  • 1
AlexW
  • 2,843
  • 12
  • 74
  • 156
  • Did you try to place a breakpoint in this return line ? Maybe there is an exception before ? – Ofiris Jul 18 '13 at 08:56
  • Put a breakpoint in your empty `catch` statements. – CodeCaster Jul 18 '13 at 09:05
  • Does just placing `return RedirectToAction("Index");` in the beginning works ? – Ofiris Jul 18 '13 at 09:06
  • ive editted the question to include the result of your suggestions, the break in the catch didnt get called and the break in the return looked normal – AlexW Jul 18 '13 at 09:34
  • are you sure your view is passing those two values`(tblEquipment Equipment, string cmd)`? May be your during submitting process of your form, you are not being forwarded to `[HttpPost]` method. – Cybercop Jul 18 '13 at 09:39
  • yes, i put a breakpoint in and double checked, plus the database is successfully updated with the edits I made, its like that return is ignored and everything else is processed... – AlexW Jul 18 '13 at 09:42
  • added some more debug, possibly an ajax issue? – AlexW Jul 18 '13 at 09:57

1 Answers1

2

sorted!

I modified the ajax options as per below, it now works succesfully

<script>
    function onSuccess() {
        window.location.href = '@Url.Action("Index","UserEquipment")'
    }
</script>


@using (Ajax.BeginForm("AddEditRecord", "UserEquipment", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "equipmentDialog", OnSuccess="onSuccess()" }))
AlexW
  • 2,843
  • 12
  • 74
  • 156
  • Of course, you can't redirect the page from the result of an AJAX call. You can let the `Add` or `Edit` method return the `Details` view for the created or edited model (or just the `Index` if you wish), so that will fill the element identified by the `AjaxOptions.UpdateTargetId`. – CodeCaster Jul 18 '13 at 11:47