AJAX를 통해 두 항목을 POST 할 컨트롤러가 있습니다. 복잡한 개체 (내 전체 viewmodel)와 정수 (특정 ID 열). 이 특정 프로젝트는 VB .Net에 있지만 누군가 C#에서 대답 할 수 있다면 괜찮을 것입니다. (두 언어 모두 상당히 잘 알고 있습니다). 두 언어 모두 작동합니다.jQuery AJAX를 사용하여 ASP .NET MVC 컨트롤러에 여러 변수 게시
컨트롤러에 viewmodel을 아무 문제없이 POST 할 수 있습니다. 정수를 포함 시키려고하면 컨트롤러는 더 이상 요청을 라우트 할 수 없습니다. 나는 그것이 내가 POST하는 데이터를 어떻게 포맷하는지에 관한 문제 일지는 모르겠지만, 내가해야 할 일을 정확히 파악할 수는 없었다.
$.ajax({
type: "POST",
url: '@Url.Action("UpdateFromDate")',
data: { viewModel : model, estimateId : 3 }
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
cache: false,
success: function (msg) {
//alert(JSON.stringify(msg));
return true;
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
//alert(errorThrown);
return false;
}
});
가 어떻게 내 뷰 모델과 정수 (3에 하드 코딩 게시 할 수 있습니다 : 같은
이<HttpPost>
Public Function UpdateFromDate(viewModel As RetirementBenefitEstimateViewModel, estimateId) As ActionResult
If viewModel IsNot Nothing AndAlso viewModel.Estimate IsNot Nothing AndAlso viewModel.Estimate.RetirementBenefitsEstimates IsNot Nothing Then
For Each item In viewModel.Estimate.RetirementBenefitsEstimates.Where(Function(est) est.EstimateId = estimateId)
' this is where I update the affected row
item.UpdateFromDate(viewModel.DateOfBirth, viewModel.EmploymentStartDate, viewModel.PersonId)
Next item
End If
' Get the previous ViewModel from session
PerformSessionAction(Of RetirementBenefitEstimateViewModel)(SessionConstants.RetirementEstimate, currentEstimate, SessionAction.GetVar)
' update it's .Estimate property
currentEstimate.Estimate = viewModel.Estimate
' save the updated ViewModel to session
PerformSessionAction(Of RetirementBenefitEstimateViewModel)(SessionConstants.RetirementEstimate, currentEstimate, SessionAction.SetVar)
' finished!
Return New HttpStatusCodeResult(HttpStatusCode.OK)
End Function
내보기에 JQuery와 AJAX 호출이 같습니다처럼
내 컨트롤러 액션 보인다 이 예)?
:
내 AJAX 호출처럼 보이는 끝납니다. 일단 그들이 가치를 얻고 귀하의 게시물에 데이터로 보낼 자바 스크립트 또는 jQuery를 사용해야합니다. – Maess
보기에서 코드를 게시하십시오. – Maess
당신이 치려고하는 URL 경로를 게시하십시오. –