2012-09-11 68 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