0

我正在处理视图中的数据表。当用户单击数据名称时,会弹出一个对话框以允许他编辑数据。当他单击删除时,会出现一个对话框提示他确认,然后删除该行。当他选择创建新行时,会弹出一个对话框让他输入新信息。在所有 3 种情况下,动作完成后,PartialView "_Content" 都会重新加载内容<div />

在整个页面加载后,这一切都在第一次正常工作。但是在 PartialView 重新加载之后(在其中一个操作之后),“编辑”对话框不再起作用,尽管其他 2 个可以。当然,我可以在每次操作后重新加载页面以重新加载所有内容,但这会更慢并且在 Ajax 世界中没有意义。如果我将编辑对话框的 JQueryUIHelper 放在局部视图中,它会再次工作,但第二次,表单会在页面上内联而不是在对话框中打开。我也直接使用 JQuery 和 JQueryUI 进行了尝试,并得到了同样的错误。我一直在研究这个并试验了好几天。

2013 年 4 月 1 日更新:*我向$.click()链接类添加了一些回调。它们在页面进行部分刷新后不起作用。我猜发生的事情是,当内容重新加载时,脚本失去了与内容 <div>中对象的“连接” 。

我通过 JQueryUIHelper 扩展使用 MVC4、Razor 和 JQueryUI。View 和 PartialView 的代码如下。

有什么想法吗??

这是我的看法

@model IEnumerable<AttributeLookup>
@{
ViewBag.Title = "Attributes";
}
<h2>
Attributes</h2>
@if (ViewBag.Error != null)
{
<div class="message-error">@ViewBag.Error</div>
}
<div id="content">
   @Html.Partial("_Content", Model)
</div>

<div style="padding-top: 12px;">
@Ajax.ActionLink("New", "Create", new { }, new AjaxOptions {
    HttpMethod = "Get",
    InsertionMode = InsertionMode.Replace,
    UpdateTargetId = "createContent"
}, new { id = "createLink" })
</div>

@using (Html.JQueryUI().Begin(new Dialog()
.Title("Confirm Delete")
.AutoOpen(false)
.Modal(true)
.CloseOnEscape(true)
.ConfirmAjax(".deleteLink", "Yes", "No",
new AjaxSettings { Method = HttpVerbs.Post, Success = "content" })))
{
<div>
    Are you sure you want to delete this attribute?
</div>
}
@using (Html.JQueryUI().Begin(new Dialog()
.Title("Create Attribute")
.AutoOpen(false)
.Width(500)
.TriggerClick("#createLink")
.Modal(true)
.CloseOnEscape(true)
.Button("OK", "save")
.Button("Cancel", "closeDialog")))
{
<div id="createContent" />
}
@using (Html.JQueryUI().Begin(new Dialog(new {id = "editDialog"})
.Title("Edit Attribute")
.AutoOpen(false)
.Width(500)
.TriggerClick(".editLink")
.Modal(true)
.CloseOnEscape(true)
.Button("OK", "save")
.Button("Cancel", "closeDialog")))
{
<div id="editContent" />
}

@section Scripts {
   <script type="text/javascript">

  var success = function(data) {
     $(window.document.body).html(data);
  };

  var content = function(data) {
     $("#content").html(data);
  };

  var closeDialog = function() {
     $(this).dialog('close');
  };

      var saveCreate = function() {
         $("#createForm").submit();
         $(this).dialog('close');
      };

      var saveEdit = function() {
         $("#editForm").submit();
         $(this).dialog('close');
      };

      $(".editLink").click(function () { alert("edit clicked"); });
      $(".deleteLink").click(function () { alert("delete clicked"); });

   </script>
} 

这是部分视图

@model IEnumerable<AttributeLookup>
@if (ViewBag.Error != null)
{
<div class="message-error">@ViewBag.Error</div>
}
<table id="attribute">
<tbody>
    <tr>
    <th style="width: 250px;">
        @Html.DisplayNameFor(model => model.Name)
    </th>
    <th style="width: 50px;">
        @Html.DisplayNameFor(model => model.Units)
    </th>
    <th style="width: 30px;">
        Contrained
    </th>
    <th style="width: 400px;">
        @Html.DisplayNameFor(model => model.Description)
    </th>
    <th>
        &#160;
    </th>
    </tr>
    @{ int count = 0; }
    @foreach (var item in Model)
    {
    string type = count % 2 == 0 ? "normal" : "alt";
    <tr class="@type">
        <td>
        @Ajax.ActionLink(@Html.DisplayFor(modelItem => item.Name).ToHtmlString(), "Edit",
        new { id = item.AttributeLookupID }, new AjaxOptions
        {
            HttpMethod = "Get",
            InsertionMode = InsertionMode.Replace,
            UpdateTargetId = "editContent"
        }, new { @class = "editLink", title = "Edit attribute" })
        </td>
        <td>
        @Html.DisplayFor(modelItem => item.Units)
        </td>
        <td>
        @if (item.AttributeConstraints != null && item.AttributeConstraints.Any())
        {
            @Html.Raw("X")
        }
        </td>
        <td>
        @Html.DisplayFor(modelItem => item.Description)
        </td>
        <td>
        @Html.ActionLink("Delete", "Delete", new { id = item.AttributeLookupID }, new { @class = "deleteLink" })
        </td>
    </tr>
        count++;
    }
</tbody>
</table>

这是编辑表单的部分内容。创建表单类似:

@model AttributeLookup
@using (Ajax.BeginForm("Edit", "AttributeLookup", new AjaxOptions {
HttpMethod = "Post",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "content"
}, new {id = "editForm"}))
{
@Html.ValidationSummary(true)

<fieldset>
    <legend>AttributeLookup</legend>
    @Html.HiddenFor(model => model.AttributeLookupID)
    <div class="editor-label">
    @Html.LabelFor(model => model.Name)
    </div>
    <div class="editor-field">
    @Html.EditorFor(model => model.Name)
    @Html.ValidationMessageFor(model => model.Name)
    </div>
    <div class="editor-label">
    @Html.LabelFor(model => model.Units)
    </div>
    <div class="editor-field">
    @Html.EditorFor(model => model.Units)
    @Html.ValidationMessageFor(model => model.Units)
    </div>
    <div class="editor-label">
    @Html.LabelFor(model => model.Description)
    </div>
    <div class="editor-field">
    @Html.EditorFor(model => model.Description)
    @Html.ValidationMessageFor(model => model.Description)
    </div>
    <div class="editor-label">
    @Html.LabelFor(model => model.AttributeConstraints, "Constraint")
    </div>
    <div class="editor-field">
    @Html.DropDownList("ConstraintTypeID")
    @Html.DropDownList("SecondaryID")
    </div>
</fieldset>
}
4

2 回答 2

1

我找到了解决方案。首先,我从 Helper 中删除了 TriggerClick:

@using (Html.JQueryUI().Begin(new Dialog(new {@id = "editDialog"})
    .Title("Edit Attribute")
    .AutoOpen(false)
    .Width(500)
    // deleted --> .TriggerClick(".editLink")
    .Modal(true)
    .CloseOnEscape(true)
    .Button("OK", "saveEdit")
    .Button("Cancel", "closeDialog")))
{
    <div id="editContent" />
}

然后我明确地将它添加到我的<scripts>

$("body").on('click', ".editLink", function () { $("#editDialog").dialog("open"); });

现在它工作正常。

于 2013-04-01T16:03:14.687 回答
0

我想知道为什么它适用于其他两个,而不是编辑一个?我怀疑这与从 id 开始的错误有关。尝试去掉 id=editdialog。这可能是一个快速修复。如果这不起作用,请继续阅读。

#dialog 通常由于在 document.ready 或后台页面加载时发生的 jqueryUi 事情而隐藏。

我不确定它何时发生,但你想在重新加载发生后重复这些步骤。在文档末尾未重新加载的部分执行类似...

  <script>
   $("body").ajaxComplete( reHideDialog())

     function reHideDialog(){
            $("#dialog").css('display','none');
     }
 </script> 

当他们单击编辑链接时,jqueryui 应自动将#dialog css 显示设置为 display:absolute,因为它会在弹出窗口中呈现它。

于 2013-03-24T01:24:28.060 回答