Saving current user to database

Topics: Customizing Orchard
Feb 28, 2015 at 1:03 PM
My Model has a FromUser and ToUser which are both a UserContentPart. When im trying to save a record in the database I set the ToUser but not the FromUser because this should be the logged in user.

Problem is that i dont know where/when to set the FromUser.

When i try to set it like this: privateMessagePart.FromUser = Services.WorkContext.CurrentUser.As<UserPart>().Record; It returns an error saying it cannot handle type UserPartRecord. What am i doing wrong?
Feb 28, 2015 at 1:24 PM
It means that the type of FromUser is not the same as UserPartRecord, so you can't assign an instance of UserPartRecord to FromUser. What is the actual type of FromUser?
Feb 28, 2015 at 1:32 PM
Thats why i dont understand the message since my model says:
    public class PrivateMessagePart : ContentPart<PrivateMessageRecord> 
        public UserPartRecord FromUser
            get { return Retrieve(r => r.FromUser); }
            set { Store(r => r.FromUser, value); }
And this is most of the controller:
                // Create in-memory part
                PrivateMessagePart privateMessagePart = Services.ContentManager.New<PrivateMessagePart>("PrivateMessage");
                if (privateMessagePart == null)
                    return HttpNotFound();

                privateMessagePart.FromUser = Services.WorkContext.CurrentUser.As<UserPart>().Record;
                // Sync in-memory part with data from form
                dynamic model = Services.ContentManager.UpdateEditor(privateMessagePart, this);
                if (!ModelState.IsValid)

                    // Casting to avoid invalid (under medium trust) reflection over the protected View method and force a static invocation.
                    return View((object)model);

                // If data from form was valid, send the message
                Services.Notifier.Information(T("Message was successfully sent"));
                return RedirectToAction("SentMessages");
Feb 28, 2015 at 1:58 PM
Oh wait, I think I know. You're using the Store and Retrieve methods to store the object into InfoSet as well as table storage. The former is only supported for simple types - you can't use it to just store any arbitrary object. You should either change your code such that it stores the ID of the user (Int32) or not use Store and Retrieve, but access the Record property directly, e.g.:
public UserPartRecord FromUser {
   get { return Record.FromUser); }
   set { record.FromUser = value); }
Feb 28, 2015 at 2:44 PM
Edited Feb 28, 2015 at 7:13 PM
Thanks, that actually explains a lot. Using that Store and Retrieve method it worked.