Gavin campbell
Gavin campbell

Reputation: 11

MVC Partial View to Call Display Pop-up window using jquery

I have a index page the renders objects from my database as a treeview, each item has a link href="/MessageGroupType/Edit/1002 that makes an Ajax call to display a partial view in a DIV.

Within the partial view there is a delete button which calls my controller to delete the item.

However, i do a check to make sure the item can be deleted, if the item cant be deleted then i wish a pop-up to appear back on the edit form telling the user they cant delete this record.

In my Edit partial view i have the following code

<asp:PlaceHolder runat="server">
        <script src="<%= Url.Content("../../Scripts/JQuery/jquery-1.4.1.min.js") %>" type="text/javascript">
        </script>
</asp:PlaceHolder>


<script type="text/javascript" >
     $(function() {
         $("#dialog").dialog();
     });

</script>

  <% if (Boolean.Parse(ViewData["DisplayWindow"].ToString())){%>
     <div id="dialog" title="Cannot Delete Message Group Type">
             <p>This Mesage group Type Cannot be deleted as is linked to other message group Types </p>
             </div>
     <% }%>

So my main questions are

  1. Can i make a reference to a javascript script within my Partial View (i dont want my master page to be called on the partial view)
  2. When i dynamically load the partial view data into my DIV - can i then after calling my controller insert another DIV into the first DIV.
  3. I am i doing this the wrong way - so any pointers is appreciated

Cheers

Upvotes: 1

Views: 2243

Answers (1)

Gregoire
Gregoire

Reputation: 24832

In your tree view, you can add an Ajax.ActionLink with a OnFailure option in AjaxOptions that will point to your $("#dialog").dialog();

In your controller, if the user can not delete the record associate a bad request code (Response.StatusCode = (int)HttpStatusCode.BadRequest;) to your HttpResponse, so your OnFailure function will be called (and your popup displayed).

Do not forget to associate a OnSuccess function to your Ajax.ActionLink if the record has been deleted

Upvotes: 1

Related Questions