0

나는 처음 업데이트 할 수있는 내용, 첫 번째 일이있는보기에서 복잡한 모델의 결합 문제, 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 컬렉션은 DiagnosisZones 사이의 중간 테이블이지만 내 모델의 원인에는 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" /> 
     &nbsp | &nbsp @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 
+0

POST를 통해 시도해 볼 수 있습니까? 또한,'arrayToParamObject'에서 결과 JSON을 보여줄 수 있습니까? – Justin

+0

"POST를 통해"무슨 뜻인지 모르겠다, arrayToParamObject'에 대한 코드는 단순히 결과를 파싱하고 잘 작동하는 자바 스크립트 함수이다. 여기에 자바 스크립트 함수가있다. function arrayToParamObject (name, array) { var obj = []; (var i = 0, len = array.length; i ApalanQ

+0

서버 (즉 JSON)에 전달되고 있는지 알아보기. 당신의 메소드가 이름 값 쌍 객체의 배열을 전달할 것이고 당신의 액션이 정수 배열을 기대하고있는 것처럼 보입니다. 어쨌든 실제 JSON이 전달되는 것을 보는 것이 훨씬 쉽습니다. 또한 ... "via POST"에 따르면, POST 메서드를 통해 데이터를 보내도록 AJAX 요청을 보내면 type : "GET"을 type : "POST"로 바꿀 수 있습니다. 당신의 행동에서 당신은'[HttpGet]'속성을''[HttpPost]'로 바꿀 것입니다. – Justin

답변

0

아닌를 : 같은

[HttpGet] 
public ActionResult GetJointBalances(int[] zonesid) { ... } 

GET 아약스 요청 데이터가 같습니다

UPDATE는 여기

서버 측 조치가 기대 것입니다 당연히, 그러나 나는 그것이하고 싶은 것을 이해하는 것을 도울 것입니다 그리고 이것을 해결하는 방법. 저스틴 말한대로 문제, 이것의 게시물 다음과 같은 주요 모델에 list<DiagZones>Diagnosis 객체를 결합했다 :와,

  • ASP.Net MVC4 bind a "create view" to a model that contains List
    • 내가 바인딩 기능을 조금 이해

      @model 목록

      @if (Model.Count() != 0) 
      { 
          <table> 
           @foreach (var dz in Model) 
           { 
            <tr> 
             <tr> 
              <td> 
               Flex: 
              </td> 
              <td> 
               Html.TextBox("Diagnosis.DiagZones[" + i + "].ext", Model[i].ext) 
              </td> 
             </tr> 
            </tr> 
      
      
           } 
          </table> 
      } 
      
      : 마음에 나는 나의 새로운 PartialView _QualityMovement.cshtml을 코딩하는 것이

      그것은 나쁜 해결책이지만 최소한 서버 측에서 제 모델을 묶었습니다. 문제는 엔티티 프레임 워크가 내 엔티티에 ICollection으로 추상화 한 것이므로 목록으로 반복 할 수 없으며 어디서나 "캐스팅"을 발견했습니다.

      요청에서 데이터를 검색하거나 알기 쉽고 이해하기 쉬운 방식으로 입력하기 위해서는 더 나은 방법이 CustomBinder 여야한다고 생각합니다.

      감사합니다. Justin 감사합니다.

    관련 문제