Question

Had a problem in creating a 1-1 relationship in orchard unable to save the value of the item selected in the drop down list . Would appriciate if someone points me the correct direction as how to go about this problem .

Contact Part Driver

 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;
            }
        }

 }

Contact Handler

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

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

        }
    }

Model 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; }

    }
}

Record Part

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

    }

}

Contact Service

 {
    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; }
    }
}

Contact View

@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>
Was it helpful?

Solution

ErMasca has rightly pointed out one mistake.

Also in your ContentPart you will need to use Record.SourceRecord to access the navigational property.

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

OTHER TIPS

http://docs.orchardproject.net/Documentation/Creating-1-n-and-n-n-relations Nevertheless in your View the dropdown is not really keeping the SourceId, isnt it? changing to :

@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...")

Will allow to be able to check against the table again.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top