Orchard CMS保存MediaPickerField

时间:2012-09-11 20:30:56

标签: c# asp.net-mvc orchardcms

我正在开发一个基于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不会保存数据。我使用调试器来查看输入[“StaffUserPart.Photo”]中的值和值是否存在。

有什么想法吗?

1 个答案:

答案 0 :(得分: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");
}