2014-01-20 117 views
2

在果園中創建1-1關係時出現問題,無法保存在下拉列表中選擇的項目的值。會appriciate如果有人指出我正確的方向,如何解決這個問題。在果園中創建1-1關係

接觸部件驅動

public class ContactPartDriver : ContentPartDriver<ContactPart> 
    { 
     private readonly IContactService _contactService; 
     public ContactPartDriver(IContactService contactService) 
     { 
      _contactService = contactService; 
     } 
     protected override string Prefix 
     { 
      get 
      { 
        return "Contact"; 
      } 
     } 
     protected override DriverResult Display(ContactPart part, 
         string displayType, dynamic shapeHelper) 
     { 
      return ContentShape("Parts_Contactindex1", 
         () => shapeHelper.Parts_Contactindex1(
          ContentPart: part, 
          Address: part.Address, 
          Name : part.Name, 
          EmailId: part.EmailId, 
          StateCode: part.Source.Id 
          )); 
     } 
     //Get 
     protected override DriverResult Editor(ContactPart part, dynamic shapeHelper) 
     { 
       return ContentShape("Parts_Contact_Edit",() => 
      shapeHelper.EditorTemplate(TemplateName: "Parts/Contactindex1", 
     Model:BuildEditorViewModel(part), Prefix: Prefix)); 
     } 
     //Post 
     protected override DriverResult Editor(ContactPart part, 
       IUpdateModel updater, dynamic shapeHelper) 
     { 
      var model = new EditContactViewModel(); 
      updater.TryUpdateModel(part, Prefix, null, null); 
      //if (part.ContentItem.Id != 0) 
      //{ 
      // _contactService.UpdateContactForContentItem(part.ContentItem, model); 

      //} 
      return Editor(part, shapeHelper); 
     } 

     private EditContactViewModel BuildEditorViewModel(ContactPart part) 
     { 
      var model = new EditContactViewModel { 
       Name = part.Name, 
       Address = part.Address, 
       EmailId = part.EmailId, 

       Sources = _contactService.GetSource() 

      }; 
      return model; 
      } 
     } 

} 

聯繫處理器

public class ContactHandler :ContentHandler 
    { 
     public ContactHandler (IRepository<ContactPartRecord> respository) 
     { 

      Filters.Add(StorageFilter.For(respository)); 

     } 
    } 

型號ContactPart

{ 
    public class ContactPart : ContentPart<ContactPartRecord> 
    { 

     public string Name 
     { 
      get { return Retrieve(x => x.Name); } 
      set { Store(x => x.Name, value); } 
     } 
     public string Address 
     { 
      get { return Retrieve(x => x.Address); } 
      set { Store(x => x.Address, value); } 
     } 
     public string EmailId 
     { 
      get { return Retrieve(x => x.EmailId); } 
      set { Store(x => x.EmailId, value); } 
     } 

     public SourceRecord Source 
     { 
      get { return Retrieve(x => x.SourceRecord); } 
      set {Store(x => x.SourceRecord,value);} 

     } 

    } 
} 

ContactPartRecord

{ 
    public class ContactPartRecord : ContentPartRecord 
    { 

     public virtual string Name { get; set; } 
     public virtual string Address { get; set; } 
     public virtual string EmailId { get; set; } 
     public virtual SourceRecord SourceRecord { get; set; } 

    } 
} 

記錄單元

{ 
    public class SourceRecord 
    { 
     public virtual int Id { get; set; } 
     public virtual string Type { get; set; } 

    } 

} 

聯繫服務

{ 
    public interface IContactService : IDependency { 
     void UpdateContactForContentItem(ContentItem item, EditContactViewModel model); 
     IEnumerable<SourceRecord> GetSource(); 
    } 

    public class ContactService : IContactService 
    { 
     private readonly IRepository<SourceRecord> _sourceRepository; 

     public ContactService(IRepository<SourceRecord> sourceRepository) 
     { 
      _sourceRepository = sourceRepository; 
     } 

     public void UpdateContactForContentItem(ContentItem item, EditContactViewModel model) 
     { 
      var contactPart = item.As<ContactPart>(); 
      contactPart.Address = model.Address; 
      contactPart.Name = model.Name; 
      contactPart.EmailId = model.EmailId; 
      contactPart.Source = _sourceRepository.Get(s => s.Id == model.SourceId); 
     } 

     public IEnumerable<SourceRecord> GetSource() 
     { 
      return _sourceRepository.Table.ToList(); 
     } 
    } 
} 

EditContactViewModel

{ 
    public class EditContactViewModel 
    { 
     public string Name { get; set; } 
     public string Address{ get; set; } 
     public string EmailId { get; set; } 
     public int SourceId { get; set; } 
     public IEnumerable<SourceRecord> Sources{ get; set; } 
    } 
} 

聯繫查看

@using Cess.Contacts.ViewModel; 
@model EditContactViewModel 

<fieldset> 
    <legend>New Contact</legend> 
    @Html.LabelFor(m =>m.Name) 
    @Html.EditorFor(m => m.Name) 
    @Html.LabelFor(m => m.Address) 
    @Html.TextAreaFor(m => m.Address) 
    @Html.LabelFor(m => m.EmailId) 
    @Html.EditorFor(m => m.EmailId) 
    <p> 
    @Html.DropDownListFor(model => model.Sources, 
         Model.Sources.Select(s => new SelectListItem 
         { 
          Selected = s.Id == Model.SourceId, 
          Text = s.Type , 
          Value = s.Id.ToString() 
         }), 
         "Choose a Source...") 
    </p> 
</fieldset> 

回答

2

ErMasca正確地指出了一個錯誤。

同樣在您的ContentPart中,您將需要使用Record.SourceRecord來訪問導航屬性。

public SourceRecord Source() 
{ 
    get { return Record.SourceRecord; } 
    set { Record.SourceRecord = value; } 
} 
2

http://docs.orchardproject.net/Documentation/Creating-1-n-and-n-n-relations 儘管如此,在你的觀點下拉並不真的保持SourceId,不是嗎? 轉到:

@Html.DropDownListFor(model => model.SourceId, 
         Model.Sources.Select(s => new SelectListItem 
         { 
          Selected = s.Id == Model.SourceId, 
          Text = s.Type , 
          Value = s.Id.ToString() 
         }), 
         "Choose a Source...") 

將允許能夠再次覈對表。

+0

其實@Yash,我認爲山姆只是做得好。 他正在使用新的Infoset模型,它應該處理所有事情。 我還沒有使用它..將盡快在我的下一個項目 在這裏theres一個小鏈接:http://weblogs.asp.net/bleroy/archive/2013/11/04/the-shift-how-orchard -parently-shifted-to-document-storage-and-how-it-ll-affect-you.aspx (對不起,我可以在你的評論下發表評論) – ErMasca

+0

是的,我知道信息轉移。我嘗試過做類似的事情。但對於關係屬性來說,你似乎必須使用Record.value – Yashvit