简体   繁体   中英

refresh partial view onclick action button

so i have the next problem. I want to refresh only a partial view but it is't working. I tried to put two action on onclick but i need to push button 3 times to refresh.

here is my partian view

<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>

and here is my 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]);
        }
    }
});
}
});

Any ideas?

Solved the problem:

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]);
        }
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM