【问题标题】:refresh partial view onclick action button刷新局部视图 onclick 动作按钮
【发布时间】:2015-08-21 07:04:21
【问题描述】:

所以我有下一个问题。我只想刷新部分视图,但它不起作用。我尝试在 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]);
        }
    }
});
}
});

有什么想法吗?

【问题讨论】:

  • 为什么你调用ShowTable 两次,同时在html 中删除一次,在ajax success 中删除一次,你的ShowTable 函数在哪里?
  • 你的ShowTable函数在哪里?
  • 已编辑,抱歉,刚刚尝试过,无法正常工作..
  • Showtable 函数在另一个 js 文件中。想法是,当我在 html 中调用函数时,我需要按 3 次按钮才能刷新
  • 那么到底发生了什么?你能详细说明一下功能吗?你的问题不清楚!

标签: javascript jquery .net asp.net-mvc model-view-controller


【解决方案1】:

解决了问题:

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

【讨论】:

    猜你喜欢
    • 2014-08-20
    • 2013-04-03
    • 2017-05-04
    • 1970-01-01
    • 2015-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多