私のコントローラーには、次のメソッドがあります..かなり簡単です。
namespace Playground.Controllers
{
public class TasksController : Controller
{
// an ajax call to this generates the server error in the response
// "Value cannot be null. Parameter name: controllerContext"
[HttpGet]
public JsonResult GetTask()
{
List<Task> tasks = GetTasks();
return Json(tasks, JsonRequestBehavior.AllowGet);
}
// an ajax call to this comes back successful, but only outputs
// "System.Collections.Generic.List`1[Playground.Models.Task]"
// which, when expanded... is empty
[HttpGet]
public List<Task> GetTasks()
{
//Create an array to hold all of the task objects
var tasks = new List<Task> { };
//Execute the select statement and get back a SqlDataReader object
DataTable table = DataAccess.ExecuteSelect("select Id, Name, Description, Starting, Ending from Tasks");
foreach (DataRow dr in table.Rows)
{
//Assign values to the task object
Task task = new Task((int)dr["Id"],
(string)dr["Name"],
(string)dr["Description"],
(DateTime)dr["Starting"],
(DateTime)dr["Ending"]);
//Add task object to list of task objects
tasks.Add(task);
}
return tasks;
}
}
}
Task 型のオブジェクトを作成します。ここに表示されるクラス
namespace Playground.Models
{
public class Task : Controller
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public DateTime Starting { get; set; }
public DateTime Ending { get; set; }
public Task(int Id, string Name, string Description, DateTime Starting, DateTime Ending)
{
this.Id = Id;
this.Name = Name;
this.Description = Description;
this.Starting = Starting;
this.Ending = Ending;
}
}
}
そのメソッドへの Ajax 呼び出しがあります。
$.ajax({
type: "GET",
url: "Tasks/GetTasks", //Changed between GetTasks and GetTask for testing.
dataType: "html",
//dataType: "json",
async: false,
data: { },
success: function (data, text) {
console.dir(data);
console.dir(text);
},
error: function (request, status, error) {
//do something
}
});
2 つの console.dir() 行からの出力:
System.Collections.Generic.List`1[Playground.Models.Task]
No Properties
Tasks.js:12
success
No Properties
Tasks.js:13
配列内の各「タスク」をループして、必要に応じて「ID」、「名前」などを出力できる Javascript オブジェクトを取得するにはどうすればよいですか?
C#でさまざまな組み合わせを試して、タスクのリストを変換して成功しませんでした
//Error on the Serialize line
// "Exception has been thrown by the target of an invocation."
public JsonResult GetTask()
{
List<Task> tasks = GetTasks();
JavaScriptSerializer ser = new JavaScriptSerializer();
object obj = tasks;
var val = ser.Serialize(obj);
return Json(val, JsonRequestBehavior.AllowGet);
}
--- AND ---
//Returns a server error to JS
//Value cannot be null. Parameter name: controllerContext
public JsonResult GetTask()
{
List<Task> tasks = GetTasks();
JsonResult jr = new JsonResult();
jr.Data = Json(tasks);
jr.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
return jr;
}
--- AND ---
//Returns a server error to JS
//Value cannot be null. Parameter name: controllerContext
public JsonResult GetTask()
{
List<Task> tasks = GetTasks();
return Json(tasks, JsonRequestBehavior.AllowGet);
}
--- AND ---
//Returns a server error to JS
//Value cannot be null. Parameter name: controllerContext
[HttpGet]
public JsonResult GetTask()
{
Task task = new Task(0, "Name", "Desc", new DateTime(), new DateTime());
return Json(task, JsonRequestBehavior.AllowGet);
}