在我的部分视图中,当我去调用控制器方法时,我收到一条无法找到匹配控制器的错误消息,但如果我在普通视图中,我可以使用
调用控制器@using (Html.BeginForm("mkadmin", "Home", FormMethod.Post))
并使用ajax调用
$.ajax({
type: "Post",
url: '@Url.Action("mkadmin", "Home")',
我需要将数据从我的局部视图上的DataTable发布到我的控制器(位于
下面)[HttpPost]
public ActionResult mkadmin(string col)
{
// do stuff
}
我创建了没有模型的局部视图。这会导致我这个问题吗?
@model IEnumerable<FutatillDataAnalytics.Models.RolesViewModel>
<table id="dataTable" class="display" style="width:100%">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Id)
</th>
<th>
@Html.DisplayNameFor(model => model.UserName)
</th>
<th>
@Html.DisplayNameFor(model => model.RoleNames)
</th>
</tr>
</thead>
<tfoot>
<tr>
<th>
@Html.DisplayNameFor(model => model.Id)
</th>
<th>
@Html.DisplayNameFor(model => model.UserName)
</th>
<th>
@Html.DisplayNameFor(model => model.RoleNames)
</th>
</tr>
</tfoot>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelitem => item.Id)
</td>
<td>
@Html.DisplayFor(modelitem => item.UserName)
</td>
<td>
@Html.DisplayFor(modelitem => item.RoleNames)
</td>
</tr>
}
</tbody>
</table>
<div id="tab" style="float: right; padding-top:20px" class="btn-group" data-toggle="buttons-radio">
<button id="btnMKAD" type="button" onclick="MakeAdmin()" class="btn btn-large btn-success " data-toggle="tab">Make Admin</button>
<button type="button"onclick="RemoveAdmin()" class="btn btn-large btn-danger" data-toggle="tab">Remove Admin</button>
</div>
<script>
//DataTable
$(document).ready(function () {
var table = $('#dataTable').DataTable({
"columnDefs": [
{
"targets": [0],
"visible": false,
"searchable": false
}]
});
// //Table Row Selection
// $('#dataTable tbody').on('click', 'tr', function () {
// $(this).toggleClass('selected');
//} );
// Single Row Selection
$('#dataTable tbody').on('click', 'tr', function () {
// get selected row data and retireve the data from col 1 which is hidden.
var selected = (table.row(this).data());
var col = selected[0];
admin(col)
if ($(this).hasClass('selected')) {
$(this).removeClass('selected');
}
else {
table.$('tr.selected').removeClass('selected');
$(this).addClass('selected');
}
});
});
function admin(col) {
$.ajax({
type: "Post",
url: '@Url.Action("mkadmin", "Home")',
data: { col },
success: function (data) {
},
error: function (data) {
$('#byCustomer').html('<h3>Error in retrieval</h3>');
}
});
};
</script>
答案 0 :(得分:1)
感谢Spivonious,我现在正在使用它,问题在于我的代码中的url我有一个拼写错误,当我写这篇文章时我纠正但从未发布那是实际问题