0

我正在开发示例 MVC 4(Razor for Views)应用程序,还使用 ​​BootStrap 和 Bootbox.js(用于显示编程对话框)。

我的视图页面(.cshtml)的底部是下面提到的用于显示模型弹出窗口的脚本(我从http://bootboxjs.com/复制了这个示例脚本:))

@section scripts 
{
 <script>
 function confirmBox() {
 $("#myModal").on("show", function () {    // wire up the OK button to dismiss the    modal when shown
 $("#myModal a.btn").on("click", function (e) {
 console.log("button pressed");   // just as an example...
 $("#myModal").modal('hide');     // dismiss the dialog
 });
 });

 $("#myModal").on("hide", function () {    // remove the event listeners when the dialog is dismissed
 $("#myModal a.btn").off("click");
 });

 $("#myModal").on("hidden", function () {  // remove the actual elements from the DOM when fully hidden
 $("#myModal").remove();
 });

  $("#myModal").modal({                    // wire up the actual modal functionality and show the dialog
  "backdrop": "static",
  "keyboard": true,
  "show": true                     // ensure the modal is shown immediately
  });
  }

 </script>

 }

下面是 HTML,它也是视图的一部分,用于显示模型对话框

 <!-- set up the modal to start hidden and fade in and out -->
 <div id="myModal" class="modal fade">
 <div class="modal-dialog">
 <div class="modal-content">
 <!-- dialog body -->
 <div class="modal-body">
 <button type="button" class="close" data-dismiss="modal">&times;</button>
 Hello world!
 </div>
  <!-- dialog buttons -->
  <div class="modal-footer"><button type="button" class="btn btn-primary">OK</button>        </div>
 </div>
 </div>
 </div>

案例 1。我的视图还有下面提到的代码,它显示了所有品牌详细信息。如果您观察到我已将 ConfirmBox() 附加到 Delete Link 的单击事件

@foreach (var item in Model)
{
 <tr>
 <td>@Html.DisplayFor(model => item.BRAND_NAME)</td>
 <td>@Html.DisplayFor(model => item.BRAND_DESCRIPTION)</td>
 <td>@Html.ActionLink("Edit", "edit", new { id = item.PK_BRAND_ID } )</td>
 <td>@Html.ActionLink("Delete" , "delete" , new { id = item.PK_BRAND_ID } , new { onclick="return confirmBox();"})</td>
 </tr>
}

案例 2.我的视图也有一个静态的 html 代码

<a href="#"onclick="return confirmBox();" > Click Me </a>

现在面临的问题是,在案例 2 中模型窗口正在显示,而在案例 1 中它没有显示。

请让我知道我在做什么?

4

1 回答 1

0

这是一个带有确认 bootBox 对话框的示例:

首先,您需要“编辑”的 Get 操作和“删除”的 ovverid Post 操作。

看法 :

@foreach (var item in Model)
    {
        <tr>
            <td>@Html.DisplayFor(model => item.BRAND_NAME)</td>
            <td>@Html.DisplayFor(model => item.BRAND_DESCRIPTION)</td>
            <td>@Html.ActionLink("Edit", "Management", new { id = item.PK_BRAND_ID })</td>
            <td>
                @using (Html.BeginForm("Delete", "Management", new { id = item.PK_BRAND_ID }, FormMethod.Post, new { id = "myForm" }))
                {
                    @Html.AntiForgeryToken()
                    @Html.HttpMethodOverride(HttpVerbs.Delete)
                    <a title="Delete" id="btnDelete">Delete</a>
                }

            </td>
        </tr>
    }

JavaScript:

<script>
    $(function () {
        $('#btnDelete').click(function () {
            bootbox.dialog({
                message: "Do you want to continue ?", title: "Management",
                buttons: {
                    main: { label: "Cancel", className: "btn btn-default", callback: function () { return true; } },
                    success: { label: "Continuo", className: "btn btn-default", callback: function () { $('#myForm').submit(); } }
                }
            });
        });
    });
</script>

控制器 :

 public class ManagementController : Controller
    {
   // GET: /Management/Edit/5
        public ActionResult Edit(long? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Brand brand = db.Brands.Find(id.Value);
            if (brand == null)
            {
                return HttpNotFound();
            }
            return View(brand);
        }

        [HttpDelete]
        [ValidateAntiForgeryToken]
        public ActionResult Delete(long? id)
        {
            Brand brand = db.Brands.Find(id.Value);
            db.Brands.Remove(person);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
    }
于 2014-12-11T17:18:42.300 回答