반응형
ASP.NET MVC의 Json 반환 결과?
JSON 결과(어레이)를 반환하려고 합니다.
수동으로 하면 동작합니다.
resources:[
{
name: 'Resource 1',
id: 1,
color:'red'
},{
name: 'Resource 2',
id: 2
}],
전달하면 렌더링에 문제가 있습니다.
보기:
resources:@Model.Resources
컨트롤러에 어떤 것이 있습니까?
public ActionResult Index()
{
...
var model = new Display();
model.Resources = GetResources();
}
public JsonResult GetResources()
{
var model = new Models.ScheduledResource()
{
id = "1",
name = "Resource"
};
return new JsonResult() { Data = model, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
모범으로
public JsonResult Resources { get; set; }
그러나 HTML로 표시되는 것을 보면:
resources:System.Web.Mvc.JsonResult
내가 어디가 잘못됐는지 알아?
다음과 같아야 합니다.
public async Task<ActionResult> GetSomeJsonData()
{
var model = // ... get data or build model etc.
return Json(new { Data = model }, JsonRequestBehavior.AllowGet);
}
또는 더 간단하게:
return Json(model, JsonRequestBehavior.AllowGet);
동작하지 않는 다른 ActionResult에서 GetResources()를 호출하고 있는 것을 알 수 있었습니다.JSON을 되찾으려면 Ajax에서 Get Resources()를 직접 호출해야 합니다.
언급URL : https://stackoverflow.com/questions/16836428/asp-net-mvc-return-json-result
반응형
'programing' 카테고리의 다른 글
플러그인과 함께 작동하도록 워드프레스 사용자 지정 테마를 수정하려면 어떻게 해야 합니까? (0) | 2023.02.28 |
---|---|
NULL 값은 데이터베이스 검색 성능에 어떤 영향을 미칩니까? (0) | 2023.02.28 |
spring webflux Functional Web Framework(reactor-netty)에서의 spring HATEOAS 사용 (0) | 2023.02.28 |
React 컴포넌트와 React 요소를 검출하는 방법 (0) | 2023.02.28 |
json gem 설치 방법 - gem 네이티브 확장을 빌드하지 못했습니다(mac 10.10). (0) | 2023.02.28 |