Jaime
Jaime

Reputation: 31

ASP.NET MVC post not working on popup window

I am using ASP.NET MVC, I have finished my project and I copied the .sln solution files to the server.

When I run it on the server, everything ok, but there is showing exceptions for the Popup forms.

I have this controller

-- The Get method
public ActionResult Remove(int id)
{
    Person Person_to_remove = new Person() { Person_Id = id };

    return View(Person);
}

-- The Post method
[ActionName("Remove"), HttpPost]
public ActionResult Remove_post(int id)
{
    DB.Remove(id);

    return RedirectToAction("Index");
}

And these views: Index View

<table>
         <tr>
            <th>Name</th>
            <th>Remove?</th>
         </tr>
    @foreach (var row in Model)
    {
         <tr>
            <td>@row.Name</td>
            <td>
              <a href= @Url.Action("Remove", new { id = row.Id }) target="popup" onclick="Open();">Remove</a>
              <script type="text/javascript">
                  function Open() {
                    window.open('@Url.Action("Remove", new { id = row.Id })', 'popup', 'width=450,height=250');
                    return false;
                  }
               </script>
            </td>
          </tr>
    }
   </table>

Remove View (As a popup window)

@using (Html.BeginForm())
{
    <main>
        <div>
            <h3>Confirm removing:</h3>
            <h1>@Model.Name</h1>
            <br />
            <div class="row">
                <div>
                    <button type="submit" onclick="updateParent();">Confirm</button> 
                    <a onclick="window.close()">Cancel</a>
                </div>
            </div>
        </div>
    </main>
}

The Index view use a default "Layout" layout and the Remove view use a "Popup" layout on the shared folder

This is the updateParent() script running on the "Popup" layout

<script type="text/javascript">
      function updateParent() {
           window.opener.location.reload();
           window.close();
      }
</script>

On my computer the app works fine, but when running on the server the popup views won't post the form, they just close without updating the data.

But when using the same URL from the Popup window into a new tab, it works. but is not desired behaviour.

I don't know what is going on, i only changed the IP in the connection string.

Upvotes: 2

Views: 2034

Answers (1)

Jaime
Jaime

Reputation: 31

The problem was that the form was closing before submitting. Thanks to derloopkat for the tip.

To solve this I change Razor @Html.BeginForm() to name the form "PopUp".

Html.BeginForm(null, null, FormMethod.Post, new { name = "PopUp", id = "PopUp" })

then modify the updateParent method, to submit the form.

<script type="text/javascript">
    function updateParent() {
        document.forms["PopUp"].submit(); <-- Added
        window.opener.location.reload();
        window.close();
    }
</script>

Don't know if it's the best way, but it works.

Upvotes: 1

Related Questions