如何从控制器获取值以查看

时间:2015-01-27 04:51:43

标签: jquery asp.net-mvc

我准备一个列表我控制器我要传递列表值以在视图中查看我有动态表在j查询中创建

我的控制器代码是

public JsonResult GetTimeTable()
    {
        List<StudentViewModel> list = new List<StudentViewModel>();
        list= studentServices.studentdata();
        return this.Json(
         new {list=list },JsonRequestBehavior.AllowGet);
    }

我的观看代码是

function GetTimeTable() { 
var URL = '@Url.Action("GetTimeTable", "StudentDetails")';
$.ajax({        
   url: URL,
    contentType: "application/json; charset=utf-8",
    type: "GET",
    dataType: "json",       
    success: function (data) {
        debugger
        var details;
        $("#dynamictable").append('<table id="table"></table>')
        var table = $("#dynamictable").children();
        table.append("<tr><th>Name</th><th>Age</th><th>Marks1</th><th>Marks2</th><th>Marks3</th></tr>");
        var row = table.append("<tr><td>Name</td><td>Age</td><td>Marks1</td><td>Marks2</td><td>Marks3</td></tr>");          
        alert('Data is inserted Sucessfully');
    },
    error: function (data) {
        alert('Data is not present ');
    }
});
}

4 个答案:

答案 0 :(得分:0)

您可以从成功函数中获取数据..像这样

var testdata = data.list;

答案 1 :(得分:0)

“数据”属于类型列表,您必须遍历该列表才能显示表格内容。

答案 2 :(得分:0)

您没有使用您返回的数据来构建表格。除非您需要返回其他数据,否则我建议您简化方法

public JsonResult GetTimeTable()
{
    return Json(studentServices.studentdata(), JsonRequestBehavior.AllowGet);
}

在success函数中,data包含您需要在循环中访问的集合(在附加表头之后)。请注意,这假定StudentViewModel包含属性NameAgeMark1等。

$.each(data, function(index, item) {
  var row = $('<tr></tr>');
  row.append($('<td></td>').text(item.Name));
  row.append($('<td></td>').text(item.Age));
  row.append($('<td></td>').text(item.Mark1));
  // ... other table columns
  table.append(row);
});

修改

除了评论之外,您的studentdata()方法需要修改如下

public List<StudentViewModel> studentdata() 
{ 
  // StudentViewModel studentViewModel = new StudentViewModel(); 
  List<StudentViewModel> list = new List<StudentViewModel>(); 
  SqlParameter[] parameters=new SqlParameter[0]; 
  DataSet dataset = DataHelper.ExecuteAdapter(CommandType.StoredProcedure,"sp_GetStudentData", parameters); 
  DataTable dataTable = dataset.Tables[0]; 
  for (int i = 0; i < dataTable.Rows.Count; i++) 
  {
    StudentViewModel studentViewModel = new StudentViewModel(); // add this
    studentViewModel.Name = dataTable.Rows[i]["Name"].ToString(); 
    studentViewModel.Age = Convert.ToInt16(dataTable.Rows[i]["Age"]); 
    studentViewModel.Marks1 = Convert.ToInt16(dataTable.Rows[i]["Marks1"]); 
    studentViewModel.Marks2 = Convert.ToInt16(dataTable.Rows[i]["Marks2"]); 
    studentViewModel.Marks3 = Convert.ToInt16(dataTable.Rows[i]["Marks3"]); 
    list.Add(studentViewModel); 
  } 
  return list; 
}

答案 3 :(得分:0)

当您以列表的形式返回数据时,属性的名称会自动列出,所以代码看起来像这样

          success : function(result){
                   $.each(result.list,function(idx,item){
                      // now the item is each row from your data sent by the controller.
                    html.append('<tr><td>' + item.Marks + '</td></tr>';
                   } 
          }