1

我正在开发一个基于 Orchard 的网络应用程序。

我正在编写一个管理员工用户的模块,这个用户是由 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 函数附加(作为一个形状)。

这是 Post 控制器:

 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 不保存数据。我使用调试器查看 input["StaffUserPart.Photo"] 中的值和值是否存在。

有任何想法吗?

4

1 回答 1

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");
}
于 2012-09-13T04:02:53.117 回答