2012-09-11 96 views
1

我正在開發基於果園一個Web應用程序。果園CMS保存MediaPickerField

我正在編寫一個管理Staff用戶的模塊,這個用戶是由UserPart和StaffUserPart組成的ContentTypes(Staff_User) - >這個部分有一個MediaPickerField。

這是在我的控制器代碼以顯示員工的用戶

public ActionResult CreateStaff() { 

     IContent staffUser = _contentManager.New("Staff_User"); 

     var model = _contentManager.BuildEditor(staffUser); 

     return View((object)model); 
    } 

確定的創建模板,我在EditorTemplates/Staff.cshtml的模板。 MediaPicker字段由BuildEditor函數附加(作爲形狀)。

這是郵政控制器:

public ActionResult CreateStaffPost(FormCollection input) { 

     IContent staffUser = _contentManager.New("Staff_User"); 

     //UserPart validation 
     if (String.IsNullOrEmpty(input["user.Email"])) 
      ModelState.AddModelError("Email", "The Email field is required."); 

     //Check if user already exits 
     var oldUser = _contentManager.Query("User").Where<UserPartRecord>(x => x.Email == input["user.Email"]) 
      .List() 
      .FirstOrDefault(); 

     if (oldUser != null) 
      ModelState.AddModelError("Email", "That email adress is already registered."); 

     if (!ModelState.IsValid) { 
      var model = _contentManager.UpdateEditor(staffUser, this); 
      return View(model); 
     } 

     StaffUserPart staff = staffUser.As<StaffUserPart>(); 
     staff.FirstName = input["FirstName"]; 
     staff.LastName = input["LastName"]; 
     staff.Location = input["Location"]; 
     staff.JobTitle = input["JobTitle"]; 
     staff.Summary = input["Summary"]; 
     staff.AreaOfExpertise = input["AreaOfExperience"]; 
     staff.Category = input["Category"]; 
     staff.Experience = input["Experience"]; 

     //Media picker field values 
     var staffImageField = (MediaPickerField)staff.Fields.Single(x => x.Name == "Photo"); 
     //TODO Fix image save during creation 
     staffImageField.Url = input["StaffUserPart.Photo.Url"]; 
     staffImageField.AlternateText = input["StaffUserPart.Photo.AlternateText"]; 
     staffImageField.Class = input["StaffUserPart.Photo.Class"]; 
     staffImageField.Style = input["StaffUserPart.Photo.Style"]; 
     staffImageField.Alignment = input["StaffUserPart.Photo.Alignment"]; 
     staffImageField.Width = String.IsNullOrEmpty(input["StaffUserPart.Photo.Width"]) ? 0 : Convert.ToInt32(input["StaffUserPart.Photo.Width"]); 
     staffImageField.Height = String.IsNullOrEmpty(input["StaffUserPart.Photo.Height"]) ? 0 : Convert.ToInt32(input["StaffUserPart.Photo.Height"]); 

     UserPart userPart = staffUser.As<UserPart>(); 
     userPart.UserName = input["user.Email"]; 
     userPart.Email = input["user.Email"]; 
     userPart.NormalizedUserName = input["user.Email"].ToLowerInvariant(); 
     userPart.Record.HashAlgorithm = "SHA1"; 
     userPart.RegistrationStatus = UserStatus.Approved; 
     userPart.EmailStatus = UserStatus.Approved; 

     //Set Password 
     _membershipService.SetPassword(userPart.As<UserPart>(), input["password"]); 

     //Create the StaffUser 
     _contentManager.Create(staffUser); 

     return RedirectToAction("Index"); 
    } 

問題

這工作的MediaPickerField沒有按;噸保存數據。我使用調試器來查看輸入[「StaffUserPart.Photo」]中的值和值是否存在。

任何想法?

+0

MediaPicker字段由BuildEditor函數(作爲一個形狀)連接...你能說明如何做到這一點嗎? – Axe

+1

據我明白,BuildEditor(ContentItem)組裝的形狀與限定該內容項的所有字段和零件。 – mberacochea

回答

1

它看起來像你做更多的工作比你需要。如果您將呼叫轉移到UpdateEditor,則此方法將執行將張貼的值放入內容的工作。您需要確保您正在實施IUpdater。另外,我添加了對ITransactionManager的依賴。我希望這將有助於抓住一些沒有擺在正確位置的東西。

public ActionResult CreateStaffPost(FormCollection input) { 

    IContent staffUser = _contentManager.New("Staff_User"); 

    //Create the StaffUser 
    _contentManager.Create(staffUser); 

    //UserPart validation 
    if (String.IsNullOrEmpty(input["user.Email"])) 
     ModelState.AddModelError("Email", "The Email field is required."); 

    //Check if user already exits 
    var oldUser = _contentManager.Query("User").Where<UserPartRecord>(x => x.Email == input["user.Email"]) 
     .List() 
     .FirstOrDefault(); 

    if (oldUser != null) 
     ModelState.AddModelError("Email", "That email adress is already registered."); 

    //This does all the work of hydrating your model 
    var model = _contentManager.UpdateEditor(staffUser, this); 
    if (!ModelState.IsValid) { 
     _transactionManager.Cancel(); 
     return View(model); 
    } 

    //Set Password 
    _membershipService.SetPassword(userPart.As<UserPart>(), input["password"]); 

    return RedirectToAction("Index"); 
} 
+1

謝謝,你的代碼片段工作得很好。 – mberacochea