基思的答案是正確的。我只是提供了一個更完整的例子。
這是控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public JsonResult WhatTimeIsIt()
{
return Json(DateTime.Now.ToString(), JsonRequestBehavior.AllowGet);
}
}
而且視圖:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<link href="~/Content/bootstrap.min.css" rel="stylesheet" />
<script src="~/Scripts/jquery-1.9.1.min.js"></script>
<script src="~/Scripts/bootstrap.min.js"></script>
<script src="~/Scripts/bootstrap-modal.js"></script>
<script type="text/javascript">
function showModal() {
$('#TheModal').modal('show');
}
function whatTimeIsIt() {
$.ajax({
url: '/home/whattimeisit',
type: 'GET'
}).done(function (data) {
showCurrentTime(data);
});
}
function showCurrentTime(data) {
$('#TheModal .modal-header h3').html('Current time and date from the server');
$('#TheModal .modal-body').html(data);
}
</script>
</head>
<body>
<button class="btn btn-primary" onclick="showModal(); return false;">Show the modal window!</button>
<div class="modal hide" id="TheModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3>This is your modal</h3>
</div>
<div class="modal-body">
Modal content goes here.
</div>
<div class="modal-footer">
<button class="btn btn-primary" onclick="whatTimeIsIt(); return false;">What time is it?</button>
</div>
</div>
</body>
</html>
注意如何事件必須由JavaScript處理。這是一個使用DOM操作的AJAX調用。
您能否介紹一些實施細節?我無法弄清楚 – Mathieu 2013-02-21 21:41:41