在 Orchard 中保存回发数据

Saving data on postback in Orchard

我是 Orchard 新手,在创建新项目时尝试获取表单数据时遇到困难。

我拥有的是一个在管理仪表板上创建菜单项的模块。该菜单项将加载一个页面,用户可以在其中输入新的 "Coach"。

教练需要 3 样东西,名字、姓氏和电子邮件。

这是我为此实现的代码...

migrations.cs

public class SDSDataMigration : DataMigrationImpl
{

    public int Create()
    {
        SchemaBuilder..CreateTable("CoachPartRecord", table => table.ContentPartRecord()
.Column("FirstName", DbType.AnsiString, c => c.WithLength(50))                                                                                   
.Column("LastName", DbType.AnsiString, c => c.WithLength(50))                                                                         
.Column("Email", DbType.AnsiString, c => c.WithLength(200)))

        ContentDefinitionManager.AlterPartDefinition("CoachPart", part => part
                                                                          .WithField("FirstName", f => f.OfType("TextField"))
                                                                          .WithField("LastName", f => f.OfType("TextField"))
                                                                          .WithField("Email", f => f.OfType("TextField"))
                                                                          );

        ContentDefinitionManager.AlterTypeDefinition("Coach", type => type.WithPart("CommonPart")
                                                                          .WithPart("CoachPart"));

        return 1;
    }

}

parts/records

public class CoachPartRecord : ContentPartRecord
{
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
    public virtual string Email { get; set; }
}

public class CoachPart : ContentPart<CoachPartRecord>
{
    public string FirstName
    {
        get { return Record.FirstName; }
        set { Record.FirstName = value; }
    }

    public string LastName
    {
        get { return Record.LastName; }
        set { Record.LastName = value; }
    }

    public string Email
    {
        get { return Record.Email; }
        set { Record.Email = value; }
    }        
}

创建编辑器的视图

@{ Layout.Title = T("Add Coach").ToString(); }
@using (Html.BeginFormAntiForgeryPost()) {
// Model is a Shape, calling Display() so that it is rendered using the most specific template for its Shape type
@Display(Model)
}

处理程序

public class CoachPartHandler : ContentHandler
{
    public CoachPartHandler(IRepository<CoachPartRecord> repository)
    {
        Filters.Add(StorageFilter.For(repository));
    }
}

driver

protected override DriverResult Editor(CoachPart part, IUpdateModel updater, dynamic shapeHelper)
    {
        updater.TryUpdateModel(part, Prefix, null, null);
        return Editor(part, shapeHelper);
    }

控制器(用于仪表板菜单项)

public ActionResult Create()
    {
        var coach = _services.ContentManager.New("Coach");
        var model = _services.ContentManager.BuildEditor(coach);
        return View(model);
    }

    [HttpPost, ActionName("Create")]
    public ActionResult CreatePOST()
    {
        var contentItem = _services.ContentManager.New("Coach");
        _services.ContentManager.Publish(contentItem);

        return View("Index");
    }

现在我可以让表格出现以创建新教练。当我点击 "Publish" 时,driver 中 CoachPart 参数的所有字段(即 FirstName、LastName、Email)均为空。

我可以查看 http 请求,也可以看到我在表单上输入的值,但它们没有进入 CoachPart。

知道为什么没有归档 CoachPart 字段吗?

谢谢!

首先,您是在自己的记录上定义属性。因此,您不需要将新的文本字段附加到您的部分,因此您应该删除它:

ContentDefinitionManager.AlterPartDefinition("CoachPart", part => part
    .WithField("FirstName", f => f.OfType("TextField"))
    .WithField("LastName", f => f.OfType("TextField"))
    .WithField("Email", f => f.OfType("TextField")));

其次,因为您使用自定义控制器而不是 orchard 的内容控制器,所以您必须实施 IUpdateModel 并对其采取行动:

[Admin]
public class MyController : Controller, IUpdateModel {
    private readonly IContentManager _contentManager;
    private readonly ITransactionManager _transactionManager;

    public MyController(IContentManager contentManager,
        ITransactionManager transactionManager) {

        _contentManager = contentManager;
        _transactionManager = transactionManager;
    }

    [HttpPost, ActionName("Create")]
    public ActionResult CreatePOST()
    {
        var contentItem = _contentManager.New<CoachPart>("Coach");

        // The implementation of IUpdateModel is necessary for this next line:
        var model = _contentManager.UpdateEditor(contentItem, this);

        if (!ModelState.IsValid) {
            _transactionManager.Cancel();
            return View(model);
        }

        _contentManager.Publish(contentItem);

        return View("Index");
    }

    bool IUpdateModel.TryUpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties) {
        return TryUpdateModel(model, prefix, includeProperties, excludeProperties);
    }

    void IUpdateModel.AddModelError(string key, LocalizedString errorMessage) {
        ModelState.AddModelError(key, errorMessage.ToString());
    }
}