单击Html.ActionLink + MVC4打开jQuery对话框
我有一个显示派对列表的视图。 每一方都有一个ActionLink。
@Html.ActionLink("Edit", "Edit", new { id = 234 })
我的操作链接转到编辑操作并呈现编辑器视图。
主要的想法是,在单击ActionLink时,应该出现一个带有编辑器视图的jQuery对话框,并且视图中的任何编辑都应该保存到数据库中。
我的问题是,我不知道如何在jQuery对话框中打开一个视图。 那你怎么在jQuery对话框中打开一个视图呢?
如果不使用ActionLink就可以实现同样的目的,这也很有帮助。
您可以让您的操作返回部分视图而不是完整视图,然后阅读jQuery UI dialog
的文档,最后编写必要的代码。
首先给你的主持人一个课:
@Html.ActionLink("Edit", "Edit", null, new { id = 234 }, new { @class = "modal" })
为对话框定义占位符:
确保您的控制器操作返回部分视图:
public ActionResult Edit(int id) { MyViewModel model = ... return PartialView(model); }
最后编写javascript以使其生效:
不用说,您需要在jquery之后包含jQuery ui脚本以及必要的样式表。
你可以这么简单
formatter: function (cellvalue, options, rowObject) { var x = '@Html.ActionLink("Col", "Index", "Lists", new { id = "listvalid" }, new { @style = "color:black;font-weight:bold;" })'; return x.replace("listvalid", rowObject.list_key).replace("Col", rowObject.list_name); }, sortable: true, align: 'left', width: 200, editable: true
你不需要做所有那些搞乱,尝试类似的东西:
@Html.ActionLink("Open Dialog", null, null, null, new { data_role = "button", data_inline = true, data_rel = "dialog", data_transition = "pop", href="#dialogId" })
这里的关键作弊是href
属性。
另请注意,您可以将对话框添加到所需页面,如下所示:
@section dialogPages { <div data-role="page" id="dialogId"> <div data-role="header"> </div> <div data-role="content"> </div> <div data-role="footer"> </div> </div> }
然后在_Layout.cshtml中包含以下内容:
@if (IsSectionDefined("dialogPages")) { @RenderSection("dialogPages") }
适合我:)