我正在尝试使用Angular/bootstrap模态框编辑MVC ApplicationUser的脚手架视图。我找到了一些示例,它们大多是jQuery编写的。我找到了一个可以使用jquery-ui很好地运行的示例。我希望与我的模态框保持一致,因此需要让它与angular-ui或普通bootstrap配合工作。我不确定这是如何调用MVC控制器进行数据绑定的。
可行的Jquery-ui示例
<script type="text/javascript">
$(document).ready(function () {
$.ajaxSetup({ cache: false });
$(".editDialog").live("click", function (e) {
var url = $(this).attr('href');
$("#dialog-edit").dialog({
title: 'Edit Customer',
autoOpen: false,
resizable: false,
height: 355,
width: 400,
show: { effect: 'drop', direction: "up" },
modal: true,
draggable: true,
open: function (event, ui) {
$(this).load(url);
},
});
$("#dialog-edit").dialog('open');
return false;
});
});
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.FullName)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Id }, new { @class = "editDialog" })|
@Html.ActionLink("Details", "Details", new { id = item.Id }) |
</td>
</tr>
}
</tbody>
<div id="dialog-edit" style="display: none"> </div>
以下是我如何使用Angular通过API调用打开模态框的方法。
$scope.editLocation = function (id) {
$scope.close();
var deferred = $q.defer();
$http({ method: 'get', url: '/api/Locations/' + id })
.success(function (model) {
deferred.resolve(model);
$scope.model = model;
}).error(function (error) {
deferred.reject(error);
}).then(function () {
$modal.open({
templateUrl: "EditLocationModal.html",
controller: 'ModalInstanceController',
resolve: {
model: function () {
return $scope.model;
}
}
});
})
return deferred.promise;
}
更新
$scope.editUser = function (id) {
$modal.open({
templateUrl: "Modals/ApplicationUserModal.html",
controller: 'ModalInstanceController',
resolve: {
model: function () {
return $scope.model;
}
}
});
};
查看
<div class="card-body card-padding" ng-controller="ApplicationUserController">
<div class="table-responsive">
<table class="table table-striped table-vmiddle">
<thead>
<tr>
<th>Full Name</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.FullName)
</td>
<td>
@Html.ActionLink("Edit", "Edit", null, new { ng_click = "editUser(item.Id)" })
</td>
</tr>
}
</tbody>
</table>
</div>
</div>
更新2 这个语法
@Html.ActionLink("Edit", "Edit", null, new { ng_click = "editUser(" + item.Id + ")" })
出现了这个错误。
错误:[$parse:syntax] 语法错误:Token 'bc05f5' 不是预期的,应该是 [)],位于表达式 [editUser(87bc05f5-35c2-4278-a528-b7e237922d4e)] 的第12列开始,而该表达式从 [bc05f5-35c2-4278-a528-b7e237922d4e)] 开始。 http://errors.angularjs.org/1.3.15/$parse/syntax?p0=bc05f5&p1=is%20unexpected%2C%20expecting%20%5B)%5D&p2=12&p3=editUser(87bc05f5-35c2-4278-a528-b7e237922d4e)&p4=bc05f5-35c2-4278-a528-b7e237922d4e)
ng_click = "editLocation(" + @item.Id + ")"
?不过我认为这可能是作用域问题。无论如何,你可以尝试完全省略变量ng_click = "editLocation()"
直到你确定它能够触发。 - Jan