MVC部分视图使用jquery调用显示弹窗
我有一个索引页面,将数据库中的对象呈现为树视图,每个项目都有一个链接 href="/MessageGroupType/Edit/1002,它进行 Ajax 调用以在 DIV 中显示部分视图。
在部分视图中,有一个删除按钮调用我的控制器来删除该项目
但是,我会进行检查以确保可以删除该项目,如果无法删除该项目,那么我希望在编辑表单上重新出现一个弹出窗口,告诉用户他们可以删除该项目。无法删除此记录。
在我的编辑部分视图中,我有以下代码
<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>
<% }%>
所以我的主要问题是
- 我可以在部分视图中引用 javascript 脚本吗(我不希望在部分视图上调用我的母版页
- )动态地将部分视图数据加载到我的 DIV 中 - 然后我可以在调用我的控制器后将另一个 DIV 插入到第一个 DIV 中吗?
- 我这样做是错误的 - 所以任何指针都值得赞赏
干杯。
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
- 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)
- 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.
- I am i doing this the wrong way - so any pointers is appreciated
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在树视图中,您可以在
AjaxOptions
中添加一个带有OnFailure
选项的Ajax.ActionLink
,该选项将指向您的$(" #dialog").dialog();
在您的控制器中,如果用户无法删除记录,则会关联一个错误请求代码 (
Response.StatusCode = (int)HttpStatusCode.BadRequest;
)到您的 HttpResponse,因此您的OnFailure
函数将被调用(并显示您的弹出窗口)。如果记录已被删除,请不要忘记将
OnSuccess
函数关联到 Ajax.ActionLinkIn your tree view, you can add an
Ajax.ActionLink
with aOnFailure
option inAjaxOptions
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 yourOnFailure
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