简体   繁体   中英

Active or Inactive on Button click in ASP.Net MVC?

I am getting return json data from server,every value is inserted in table except status.

<script>
    $(document).ready(function () {

        $("#DomainID").change(function () {

            var id = $(this).val();
            $("#example tbody tr").remove();

            $.ajax({

                type: 'POST',

                url: '@Url.Action("ViewModules")',
                dataType: 'json',
                data: { id: id },
                success: function (data) {
                    var items = '';
                    $.each(data.EmpList, function (i, item) {
                        $("#findValue").show();

                        /*Find Role here - Comparing Emp List ModuleId to RoleList ModuleId*/

                        var RoleName = $(data.role).filter(function (index, item) {
                            return item.ModuleID == item.ModuleID
                        });

                        if (item.ParentModuleID == -1) {

                            item.ModuleName = " -- " + item.ModuleName
                        }
                        else {
                            item.ModuleName = item.ModuleName
                        }

                        if (item.Status == "Y") {
                            item.Status = + '<a href="/Account/Enable/' + item.ModuleID + '"><img src="~/img/Active.png" height="32" width="32"/></a>'
                        }
                        else (item.Status == "N")
                        {
                            item.Status = + '<a href="/Account/Enable/' + item.ModuleID + '"><img src="~/img/InActive.png" height="32" width="32"/></a>'

                        }

                        var t = i + 1;
                        var rows = "<tr>"
                        + "<td>" + t + "</td>"
                        + "<td>" + item.ModuleName + "</td>"
                        + "<td>" + item.Url + "</td>"
                        + "<td>" + RoleName[i].RoleName + "</td>"
                        + "<td>" + '<a href="@Url.Action("EditModules", "Account")?id=' + item.ModuleID + '" class="font-icon font-icon-pencil"></a>' + item.Status + "</td>"
                            + "</tr>";
                        $('#example tbody').append(rows);
                    });
                },
                error: function (ex) {
                    var r = jQuery.parseJSON(response.responseText);
                    alert("Message: " + r.Message);
                    alert("StackTrace: " + r.StackTrace);
                    alert("ExceptionType: " + r.ExceptionType);
                }
            });
            return false;
        });
    });
</script>

}

if item.Status == "N" means InActive image will display and if item.Status == "Y" means Active image will display

But in my code Status Value i didn't get any idea.?

Controller:

public ActionResult ViewModules(int id)
        {
            Domain_Bind();
            dynamic mymodel = new ExpandoObject();
            userType type = new userType();


            List<ViewRoleModules> EmpList = type.GetRoleModulesViews(id);
            List<ViewRoleModules> RoleList;
            List<ViewRoleModules> role = new List<ViewRoleModules>();
            foreach (ViewRoleModules emp in EmpList)
            {
                RoleList = type.GetSiteRoleModulesViews(emp.ModuleID);
                foreach (ViewRoleModules vip in RoleList)
                {
                    role.Add(new ViewRoleModules
                    {
                        RoleName = vip.RoleName,
                        ModuleID = vip.ModuleID
                    });
                }
            }

            var data = new { EmpList = EmpList, role = role };

            return Json(data, JsonRequestBehavior.AllowGet);

        }

表

Your code is a bit of a mess to be honest with several syntax errors. Hope this helps:

       $.ajax({
            type: 'POST',
            url: '@Url.Action("ViewModules")',
            dataType: 'json',
            data: { id: id },
            success: function (data) {
                $.each(data.EmpList, function (i, item) {
                    $("#findValue").show();
                    var roleName = $(data.role).filter(function (index, item) {
                        return item.ModuleID == item.ModuleID
                    });

                    var moduleName = item.ModuleName;
                    if (item.ParentModuleID == -1) {
                        moduleName = " -- " + moduleName;
                    }

                    var status = '';
                    if (item.Status == "Y") {
                        status = '<a href="/Account/Enable/' + item.ModuleID + '"><img src="~/img/Active.png" height="32" width="32"/></a>';
                    } else {
                        status = '<a href="/Account/Enable/' + item.ModuleID + '"><img src="~/img/InActive.png" height="32" width="32"/></a>';
                    }

                    var row = "<tr>" +
                    "<td>" + (i + 1) + "</td>" +
                    "<td>" + moduleName + "</td>" +
                    "<td>" + item.Url + "</td>" +
                    "<td>" + roleName[i].RoleName + "</td>" +
                    "<td>" + status + "</td>" +
                    "</tr>";
                    $('#example tbody').append(row);
                });
            },
            error: function (ex) {
                var r = jQuery.parseJSON(response.responseText);
                alert("Message: " + r.Message);
                alert("StackTrace: " + r.StackTrace);
                alert("ExceptionType: " + r.ExceptionType);
            }
        });

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