javascript - 如何使用asp.net mvc从返回JSON值中查看表

标签 javascript jquery asp.net ajax asp.net-mvc

当下拉菜单onchange id传递到数据库并返回相应字段时

<div class="col-lg-4">
                        <fieldset class="form-group">
                            <label class="form-label" for="exampleInput">Domain Name</label>
                            @Html.DropDownList("DomainID", null, "--- Select Domain Name ---", new { @class = "select2-arrow" })
                        </fieldset>
                    </div>

返回的JSON值我们需要查看表中对应的字段。

<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, function (i, item) {

                            var rows = "<tr>"
                            + "<td>" + item.ModuleName + "</td>"
                            + "<td>" + item.Url + "</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>

表格代码

<table id="example" class="display table table-bordered" cellspacing="0" width="100%;">
                        <thead>
                            <tr>
                                @*<th>S#</th>*@
                                <th>Module Name</th>
                                <th>Url</th>
                                @*<th>Roles</th>
                                <th>Action</th>*@
                            </tr>
                        </thead>
                        <tbody>


                        </tbody>
                    </table>

Controller 代码:

[HttpPost]
        [MyExceptionHandler]
        public ActionResult ViewModules(int id)
        {
            Domain_Bind();
            dynamic mymodel = new ExpandoObject();
            userType type = new userType();
            mymodel.viewRoleModules = type.GetRoleModulesViews(id);
            return Json(mymodel, JsonRequestBehavior.AllowGet);

        }

在上面的代码中,它返回 3 个数组值 return Json(mymodel, JsonRequestBehavior.AllowGet);

我们已传递给 JavaScript,但它显示未定义

array

在上图中,您可以看到有 3 个值,但我无法传递行内存在的 item.ModuleName

unknown

当函数成功时,该值也显示未定义。

public List<ViewRoleModules> GetRoleModulesViews(int id)
            {
                using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Admin"].ConnectionString))
                {
                    List<ViewRoleModules> EmpList = new List<ViewRoleModules>();
                    SqlCommand com = new SqlCommand("MEDEIL_Modules_SelectDomainModules", conn);
                    com.CommandType = CommandType.StoredProcedure;
                    com.Parameters.AddWithValue("@DomainID", id);
                    SqlDataAdapter da = new SqlDataAdapter(com);
                    DataTable dt = new DataTable();

                    conn.Open();
                    da.Fill(dt);
                    conn.Close();
                    foreach (DataRow dr in dt.Rows)
                    {

                        EmpList.Add(

                            new ViewRoleModules
                            {
                                ModuleID = Convert.ToInt32(dr["ModuleID"]),
                                CompanyTypeID = Convert.ToInt32(dr["CompanyTypeID"]),
                                DomainID = Convert.ToInt32(dr["DomainID"]),
                                ParentModuleID = Convert.ToInt32(dr["ParentModuleID"]),
                                ModuleName = Convert.ToString(dr["ModuleName"]),
                                FolderName = Convert.ToString(dr["FolderName"] == DBNull.Value ? null : dr["FolderName"].ToString()),
                                Url = Convert.ToString(dr["Url"]),
                                TabOrder = Convert.ToInt32(dr["TabOrder"]),
                                Style = Convert.ToString(dr["Style"]),
                                Status = Convert.ToString(dr["Status"]),
                                IsTab = Convert.ToString(dr["IsTab"]),
                                ApprovalProcess = Convert.ToString(dr["ApprovalProcess"]),
                                CreatedBy = Convert.ToInt32(dr["CreatedBy"] == DBNull.Value ? null : dr["CreatedBy"].ToString()),
                                CreatedDate = Convert.ToDateTime(dr["CreatedDate"]),
                                ModifiedBy = Convert.ToInt32(dr["ModifiedBy"] == DBNull.Value ? null : dr["ModifiedBy"].ToString()),
                                ModifiedDate = Convert.ToDateTime(dr["ModifiedDate"] == DBNull.Value ? null : dr["ModifiedDate"].ToString())
                            }
                        );
                    }

                    return EmpList;
                }
            }

最佳答案

试试这个

 public ActionResult ViewModules(int id) 
    { 
      Domain_Bind(); 
      userType type = new userType(); 
      List<ViewRoleModules> EmpList = type.GetRoleModulesViews(id); 
      return Json(EmpList, JsonRequestBehavior.AllowGet); 
    }

提供返回类型而不是使用动态

关于javascript - 如何使用asp.net mvc从返回JSON值中查看表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47667937/

相关文章:

javascript - 我需要将一个变量传递给我的函数

c# - 托管在 IIS/Asp.NET 应用程序中的 HttpClient 证书不起作用

javascript - 如何模拟 ES6 模块的导入?

javascript - 从自定义状态变量切换状态

javascript - 如何在多个 jQuery 函数之间进行交互并在单击 jQuery 时更改背景图像?

jquery - jQuery和HTML 5 <audio>

javascript - 检查 http 响应代码以显示适当的失败消息

javascript - Lodash:通过比较 id 和其他属性来删除重复项

asp.net - 有没有适用于 jquery/asp.net 的开源 Ajax 调度程序?

asp.net - 将 Windows 凭据传递给远程 WCF 服务