所以我有下一个问题。我想只刷新部分视图,但它不起作用。我尝试在onclick上放两个动作,但我需要按下按钮3次才能刷新。
这是我的伙伴观点
<table>
<tr>
<td>Id</td>
<td>UserName</td>
<td>FirstName</td>
<td>LastName</td>
<td>Email</td>
<td>Rolul</td>
<td></td>
</tr>
@foreach (var item in Model.users)
{
<tr>
<td>@item.UserID</td>
<td>@item.UserName</td>
<td>@item.FirstName</td>
<td>@item.LastName</td>
<td>@item.Email</td>
<td>@item.RoleId</td>
@if (item.RoleId == 2)
{
<td><input type="button" value="MakeAdmin" onclick="makeUserAdmin(@item.UserID);"/></td>
}
@if(item.RoleId==1)
{
<td><input type="button" value="RemoveAdmin" onclick="removeUserAdmin(@item.UserID);ShowTable('@Url.Action("UserTableView","User")" /></td>
}
@if (item.RoleId == 3)
{
<td><input type="button" disabled value="SuperAdmin" /></td>
}
</tr>
}
</table>
这是我的js
$(document).ready(function () {
function makeUserAdmin(userId) {
$.ajax({
type: 'post',
url: '/User/MakeAdmin',
data: JSON.stringify
({
userId: userId
}),
contentType: 'application/json;charset=utf-8',
dataType: 'json',
success: function (data) {
if (data.Succes) {
alert("..");
}
else {
alert(data.Errors[0]);
}
}
});
}
function removeUserAdmin(userId) {
$.ajax({
type: 'post',
url: '/User/RemoveUserAdmin',
data: JSON.stringify
({
userId: userId
}),
contentType: 'application/json;charset=utf-8',
dataType: 'json',
success: function (data) {
if (data.Succes) {
alert("...");
}
else {
alert(data.Errors[0]);
}
}
});
}
});
有什么想法吗?
答案 0 :(得分:0)
解决了问题:
function makeUserAdmin(button, userId) {
$.ajax({
type: 'post',
url: '/User/MakeAdmin',
data: JSON.stringify
({
userId: userId
}),
contentType: 'application/json;charset=utf-8',
dataType: 'json',
success: function (data) {
if (data.Success) {
console.log($(button));
$(button).attr('onclick', 'removeUserAdmin(this,' + userId + ')');
$(button).val("RemoveAdmin");
alert("..");
}
else {
alert(data.Errors[0]);
}
}
});
function removeUserAdmin(button, userId) {
$.ajax({
type: 'post',
url: '/User/RemoveUserAdmin',
data: JSON.stringify
({
userId: userId
}),
contentType: 'application/json;charset=utf-8',
dataType: 'json',
success: function (data) {
if (data.Success) {
console.log($(button));
$(button).attr('onclick', 'makeUserAdmin(this,' + userId + ')');
$(button).val("MakeAdmin");
alert("...");
}
else {
alert(data.Errors[0]);
}
}
});