2017-06-27 75 views
0

我试图做一个回调到服务器返回一个部分视图到我的模态,当我点击“添加角色”按钮,但没有任何反应,当我点击按钮。使用ajax回调到服务器

JavaScript的:

$(".addRole").on("click", function(){ 
    bootbox.dialog({ 
    title: "Create new role", 
    callback: function() { 
     $.ajax({ 
     url: "/Uam/CreateRole/", 
     method: "POST", 
     success: function(){} 
     }); 
    } 
    }); 
}); 

检视:

@model List<Vidly.Models.AvailableRole> 

@{ 
ViewBag.Title = "CreateRole"; 
Layout = "~/Views/Shared/_Layout.cshtml"; 
} 

<h2>Available Roles</h2> 

<div id="addRoleForm"> 
    <button class="btn btn-success pull-right addRole" type="button" data-toggle="modal">New Role</button> 
</div> 

控制器:

public ActionResult CreateRole(int? id){ 
    if (id == null){ 
    var menuViewModel = new RoleMenuViewModel{ 
     Menus = GetMultiselectItems() 
    }; 
    return View("SaveRole", menuViewModel); 
    } 
+1

你怎么在开发工具看? – SLaks

+1

第一步应始终是在浏览器中查看控制台(F12或Ctrl + Shift + I取决于浏览器)。我推荐Chrome,因为它有很棒的开发工具。 – Clonkex

+0

谢谢。控制台说“未捕获的错误:请指定一条消息” – user8175382

回答

0

消息属性是强制为bootbox对话框

$(".addRole").on("click", function(){ 
    bootbox.dialog({ 
    message: '<p class="text-center">Please wait while Createing new role...</p>', 
    title: "Create new role", 
    callback: function() { 
     $.ajax({ 
     url: "/Uam/CreateRole/", 
     method: "POST", 
     success: function(){} 
     }); 
    } 
    }); 
}); 

http://bootboxjs.com/examples.html#bb-custom-dialog

+0

谢谢,但我实际上试图调用CreateRole来返回部分视图。无论如何,我可以在Bootbox的消息属性中显示这个?我接受其他选择。 – user8175382

0

我最终使用的基本模式引导与模态身体@ html.RenderAction呼叫和发射按钮的引用。没有使用JQuery。

<button id="addRole-btn" class="btn btn-success pull-right btn-lg" data-toggle="modal" data-target="#modal-container">New Role</button> 


<div id="modal-container" class="modal fade" role="dialog" width="500px"> 
<div id="role-container"></div> 
<div class="modal-content"> 
    <div class="modal-body"> 
     @if (true) 
     { 
      Html.RenderAction("CreateRole", "Uam"); 
     } 
    </div> 
</div>