2013-02-07 2 views
4

검도 그리드의 모델 설정에서 json 결과의 하위 등록 정보에 열/필드를 바인딩하는 방법 (자바 스크립트 사용)? 예를 들어, 격자에 열 (FName, LName, Street 및 Address)이 포함되도록합니다. 기본적으로 나는 웹 서비스에 의해 반환 된 계층 구조를 평평하게하고 싶다.검도 UI 그리드 - 하위 등록 정보에 바인딩하는 방법

검도 설정

fields: { 
    FName: { type: "string" }, 
    LName: { type: "string" }, 
    // How to map to child properties below? 
    Street: { field: "Address.Street" }, // this is wrong    
    City: { field: "Address.City" }   // this is wrong 
} 

JSON은

{ 
    "FName": "William", 
    "LName ": "Shakespeare",    
    "Address": 
      { 
      "Address": "123 Street Ln", 
      "City": "Philadelphia" 
      } 
} 

답변

3

당신은 그런 식으로하지 않습니다. 데이터 그래프를 평평하게하는 'Model'클래스를 생성해야합니다. 모델을 생성하는 동안 지연로드를 사용할 수 있습니다. 이 모델을 컨트롤러를 통해보기로 보내거나보기로 전송되는 더 큰 ViewModel (MVVM이 아닌 모델의 모델)에 연결하십시오. 그런 다음 이것을 Grid에 바인딩하십시오.

하지만 JSON과 동일한 모델의 Ajax 로딩을 사용하는 것이 더 행복 할 것입니다. 이것이 내가하려는 생각입니다.

모델 이제까지는 필라델피아로 만든 것

public JsonResult ReadContacts([DataSourceRequest]DataSourceRequest request) 
{ 
    var contacts = _contactsDataProvider.Read(); // Your database call, etc. 
    DataSourceResult result = ContactModel.FlattenToThis(contacts).ToDataSourceResult(request); 
    return Json(result, JsonRequestBehavior.AllowGet); 
} 

는하지만 생각하지 않는다

public class ContactModel 
{ 
    public string FName { get; set; } 
    public string LName { get; set; } 
    public string Address { get; set; } 
    public string City { get; set; } 

    public ContactModel() 
    {} 
    public ContactModel(Contact contact) // IContact is better if you have Interfaces 
    { 
     FName = contact.FName; 
     LName = contact.LName; 
     Address = contact.Address.Address; 
     City = contact.Address.City; 
    } 

    // Neat Linq trick to convert database query results directly to Model 
    public static IList<ContactModel> FlattenToThis(IList<Contact> contacts) 
    { 
     return contacts.Select(contact => new ContactModel(contact)).ToList(); 
    } 
} 

컨트롤러. ;)