나는 처음 업데이트 할 수있는 내용, 첫 번째 일이있는보기에서 복잡한 모델의 결합 문제, entites 있습니다MVC 3 - 엔티티 프레임 워크 - 아약스 - 호출 부분보기에서 포스트 데이터
public partial class Diagnosis
{
public Diagnosis()
{
this.DiagZones = new HashSet<DiagZone>();
...
}
public int diagnosisid { get; set; }
public int playerid { get; set; }
public int userid { get; set; }
...
public virtual ICollection<DiagZone> DiagZones { get; set; }
}
내 DiagZones
컬렉션은 Diagnosis
과 Zones
사이의 중간 테이블이지만 내 모델의 원인에는 ID보다 많은 필드가 있습니다.
Zones
을 선택하거나 선택 취소 할 수있는 선택 컨트롤이 있습니다. onchange
이 발생하면 부분 뷰를 아약스 호출로 가져옵니다.
코드 :
EditDiagnosis.cshtml
@model Gfire.Models.DiagnosisViewModel
<h2>
@ViewBag.playername</h2>
@using (Html.BeginForm("EditDiagnosis", "Diagnosis", FormMethod.Post))
{
@Html.HiddenFor(d => d.Diagnosis.diagnosisid)
<table>
...
</table>
<table>
<tr>
<td>
Zones:
</td>
<td>
@Html.ListBoxFor(d => d.SelectedZones, new SelectList(Model.Zones, "zoneid", "description"), new
{
style = "width:305px;",
onchange = "QualityMovement(this);",
id = "lbZone",
@class = "chzn-select"
})
</td>
<td>
...
</td>
</tr>
</table>
<div id="qualitymovement">
@Html.Partial("_QualityMovement", Model.Diagnosis.DiagZones)
</div>
<div>
<input type="submit" value="Save" />
  |   @Html.ActionLink("Cancel", "IndexDiagnosisPlayer", new { playerid = ViewBag.playerid })
</div>
}
부분보기 (_QualityMovement.cshtml) :
@model List<Gfire.Domain.Entities.Diagnosis.DiagZone>
@if (Model.Count() != 0)
{
<table>
@foreach (var dz in Model)
{
<tr>
<tr>
<td>
Flex:
</td>
<td>
@Html.TextBoxFor(d => dz.flex))
</td>
</tr>
</tr>
}
</table>
}
Ajax.Get
전화 :
<script type="text/javascript" language="javascript">
function JointBalance(item) {
...
$.ajax({
url: '@Url.Action("GetJointBalances", "Diagnosis")',
data: arrayToParamObject('zonesid', items),
contentType: "application/json; charset=utf-8",
success: function (data) {
// Successful requests get here
$("#jointbalance").html(data);
$("#jointbalance").fadeIn('slow');
},
type: "GET",
datatype: "json"
});
...
}
</script>
서버에서 새 메서드 DiagZones
을 초기화하고 EditView.cshtml
을 올바르게 업데이트하는 메서드가 있습니다. 나는 모든 필드와 DiagZones 목록하지만 내 방식으로 전체 Diagnosis
객체를 제출하려고 할 때
문제
온다 :[HttpPost]
public ActionResult EditDiagnosis(DiagnosisViewModel DiagnosisViewModel)
{
if (ModelState.IsValid)
{
// Save the model
...
return RedirectToAction("IndexDiagnosisPlayer", new { playerid = SessionHelper.Player.playerid });
}
else
{
return View("EditDiagnosis", new { diagnosisid = DiagnosisViewModel.diagnosisid });
}
}
내 모델은 DiagnosisViewModel.DiagZones
목록을 비울 수 있습니다.
EditorFor를 사용하여 전체 모델을 부분보기로 전달하고 여러 양식을 추가하려했지만 쓸모가 없었습니다. 어떻게 그 목록을 내 모델에 바인딩 할 수 있습니까?
미리 감사드립니다. 나는 임시 해결책을 발견
Request URL:http://localhost/Gfire.WebUI/Diagnosis/GetQualityMovement?zonesid=47
Request Method:GET
Status Code:200 OK
Request Headersview source
...
Connection:keep-alive
Content-Type:application/json; charset=utf-8
...
Referer:http://localhost/Gfire.WebUI/Diagnosis/EditDiagnosis?diagnosisid=0&playerid=23
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.152 Safari/537.22
X-Requested-With:XMLHttpRequest
Query String Parametersview sourceview URL encoded
zonesid:47
POST를 통해 시도해 볼 수 있습니까? 또한,'arrayToParamObject'에서 결과 JSON을 보여줄 수 있습니까? – Justin
"POST를 통해"무슨 뜻인지 모르겠다, arrayToParamObject'에 대한 코드는 단순히 결과를 파싱하고 잘 작동하는 자바 스크립트 함수이다. 여기에 자바 스크립트 함수가있다. function arrayToParamObject (name, array) { var obj = []; (var i = 0, len = array.length; i
ApalanQ
서버 (즉 JSON)에 전달되고 있는지 알아보기. 당신의 메소드가 이름 값 쌍 객체의 배열을 전달할 것이고 당신의 액션이 정수 배열을 기대하고있는 것처럼 보입니다. 어쨌든 실제 JSON이 전달되는 것을 보는 것이 훨씬 쉽습니다. 또한 ... "via POST"에 따르면, POST 메서드를 통해 데이터를 보내도록 AJAX 요청을 보내면 type : "GET"을 type : "POST"로 바꿀 수 있습니다. 당신의 행동에서 당신은'[HttpGet]'속성을''[HttpPost]'로 바꿀 것입니다. – Justin