0

我正在尝试从使用 ViewData 填充 linq 数据的通用列表中提取数据,并使用 jquery .append 在 View 中发布检索到的数据,示例:

看法

@{

IList<ProjectName.Models.Employees> deserialize = ViewData["Employees"] as      
  List<ProjectName.Models.Employees>;

 }
     <!DOCTYPE html>

<html>
<head>
    <title>Index</title>
</head>
<body>
    <script src="../../Scripts/jquery-1.7.1.min.js"></script>

<script type="text/javascript">
    $(function () {
        $('#ViewData').click(function () {
            $("#employee").append("<tr><td>" + "@deserialize[1].FirstName.ToString()" +    "     </td></tr>");

        });
    });
</script>
<div>
       <button id="ViewData">View Data</button>

        <table id="employee">

        </table>

    </div>
</body>
</html>

控制器

Public ActionResult Index()
    {
        var empList = new List<ProjectName.Models.Employees>();
        var emps = from emp in _employees.Employees

                   select new Models.Employees
                   {
                       FirstName = emp.FirstName,
                       LastName = emp.LastName,
                       Title = emp.Title
                   };

        ViewData["Employees"] = emps.ToList();           
        return View(emps);           
    }

模型

Public class Employees
{
    public string FirstName
    {
        get;
        set;
    }
    public string LastName
    {
        get;
        set;
    }
    public string Title
    {
        get;
        set;
    }
}

问题

如何使用属性(名字、姓氏和标题)获取每条记录或用户,并使用 jquery 将每个检索到的值附加到 View 中的表上

4

1 回答 1

1

有几种方法可以完成此任务,最好的方法是,

    @{

    IList<ProjectName.Models.Employees> deserialize = ViewData["Employees"] as      
      List<ProjectName.Models.Employees>;

     }
         <!DOCTYPE html>

    <html>
    <head>
        <title>Index</title>
    </head>
    <body>
        <script src="../../Scripts/jquery-1.7.1.min.js"></script>

    <script type="text/javascript">
        $(function () {
            $('#ViewData').click(function () {
               $('#employee').toggle('slow');
            });
        });
    </script>
    <div>
           <button id="ViewData">View Data</button>

            <table id="employee" style="display:none">
    @foreach(var item in deserialize)
{
<tr><td>@item.FirstName.ToString()</td></tr>
}
            </table>

        </div>
    </body>
    </html>

希望这可以帮助

于 2013-09-27T06:44:43.060 回答