2012-12-01 2 views
1

json 개체에 asp.net mvc 컨트롤러 동작으로 전달 중입니다. 두 매개 변수는 모두 널입니다.여러 Json 개체가 HttpPost에서 asp.net mvc 컨트롤러 동작에 바인딩되지 않습니다.

누군가가 실수로 잘못 명명 한 오류를 발견 할 수 있습니까?

/* Source Unit */ 
var sourceParent = sourceNode.getParent(); 
var sourceUnitParentId = sourceParent == null ? null : sourceParent.data.key; 
var sourceUnit = { unitId: sourceNode.data.key, parentId: sourceUnitParentId }; 
var sourceUnitJson = JSON.stringify(sourceUnit); 

/* Target Unit */ 
var targetParent = targetNode.getParent(); 
var targetUnitParentId = targetParent == null ? null : targetParent.data.key; 
var targetUnit = { unitId: targetNode.data.key, parentId: targetUnitParentId }; 
var targetUnitJson = JSON.stringify(targetUnit); 

moveUnit(sourceUnitJson, targetUnitJson); 



function moveUnit(sourceUnit, targetUnit) { 
     $.ajax({ 
      url: '@Url.Action("Move", "Unit")', 
      type: 'POST', 
      data: { sourceUnit: sourceUnit, targetUnit: targetUnit }, 
      success: function (response) { 

      }, 
      error: function (e) { 

      } 
     }); 
    } 

[HttpPost] 
     public ActionResult Move(DragDropUnitViewModel sourceUnit, DragDropUnitViewModel targetUnit) 
     { 
      Unit sUnit = Mapper.Map<DragDropUnitViewModel, Unit>(sourceUnit); 
      Unit tUnit = Mapper.Map<DragDropUnitViewModel, Unit>(targetUnit); 
      _unitService.MoveUnit(sUnit, tUnit); 
      return new EmptyResult(); 
     } 
+0

나는 node라는 라벨이 붙은 것을보고 dom 요소라고 가정합니다. 아마 돔이 준비되기 전에 그걸 잡을거야? – MushinNoShin

+0

@MushinNoShin 노드 물건은 jquery dynatree 플러그인에 있습니다. 여기서는 아무런 역할을하지 않습니다. 그것은 MVC 컨트롤러의 동작에 여러 json 객체를 전달하는 것입니다. – Elisabeth

답변

2

왜 뷰 모델을 사용하지 않습니까? JSON을 컨트롤러 액션에 전달하려면 2 개의 속성을 포함하는 뷰 모델을 정의한 다음 전체 요청을 JSON.stringify으로 정의하십시오.

다음은 뷰 모델입니다 :

public class MoveViewModel 
{ 
    public DragDropUnitViewModel SourceUnit { get; set; } 
    public DragDropUnitViewModel TargetUnit { get; set; } 
} 

이제 인수로 뷰 모델을 취할 것 컨트롤러의 액션은 다음과 같다 :

[HttpPost] 
public ActionResult Move(MoveViewModel model) 
{ 
    Unit sUnit = Mapper.Map<DragDropUnitViewModel, Unit>(model.SourceUnit); 
    Unit tUnit = Mapper.Map<DragDropUnitViewModel, Unit>(model.TargetUnit); 
    _unitService.MoveUnit(sUnit, tUnit); 
    return new EmptyResult(); 
} 

을 마지막으로 AJAX를 사용하여이 컨트롤러 액션을 호출하고 확인하여 JSON 요청을 보내 올바른 요청 콘텐츠 유형을 지정했는지 확인합니다. 그렇지 않으면 ASP.NET MVC는 JSON 요청을 deserialize하는 방법을 알 수 없습니다.

/* Source Unit */ 
var sourceParent = sourceNode.getParent(); 
var sourceUnitParentId = sourceParent == null ? null : sourceParent.data.key; 
var sourceUnit = { unitId: sourceNode.data.key, parentId: sourceUnitParentId }; 


/* Target Unit */ 
var targetParent = targetNode.getParent(); 
var targetUnitParentId = targetParent == null ? null : targetParent.data.key; 
var targetUnit = { unitId: targetNode.data.key, parentId: targetUnitParentId }; 

/* build the view model */ 
var moveModel = { sourceUnit: sourceUnit, targetUnit: targetUnit }; 

/* Pass the view model to the server using an AJAX request */ 
moveUnit(moveModel); 



function moveUnit(moveModel) { 
    $.ajax({ 
     url: '@Url.Action("Move", "Unit")', 
     type: 'POST', 
     // It's very important to specify the correct content type 
     // request header because we are sending a JSON request 
     contentType: 'application/json; charset=utf-8', 
     data: JSON.stringify(moveModel), 
     success: function (response) { 

     }, 
     error: function (e) { 

     } 
    }); 
} 

요약 :

  • 당신이 컨트롤러 액션 당신이 잘못을하고있는 1 개 이상의 하나의 인자를 가지고있을 때마다. 즉시 뷰 모델을 중지하고 정의하십시오.
  • 컨트롤러 동작을 통해 도메인 모델을보기로 전달할 때마다 잘못 수행하는 경우가 있습니다. 즉시 뷰 모델을 중지하고 정의하십시오.

보시다시피 ASP.NET MVC의보기 모델에 대한 것입니다.

+0

Hey Darin, 이전에 제 2 DragDropUnitViewModel 인스턴스 주위에 "래퍼"를 사용했습니다. 래퍼의 이름을 뷰 모델로 지정합니다. 나는 동의 할 것이다. 멋지게 운동하지 못했지만 내 실수였습니다. 예 물론 당연히 항상 viewmodels를 사용합니다 ;-) 코드 작업에 감사드립니다. – Elisabeth

관련 문제