在Orchard中保存回发数据

时间:2017-06-08 20:05:44

标签: orchardcms

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

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

教练需要3件事,名字,姓氏和电子邮件。

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

migrations.cs

        <td><? echo $row['miu'];?>&nbsp;</td>

份/记录

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;
    }

}

查看创建编辑器

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));
    }
}

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

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

现在我可以让表单显示创建一个新教练。当我点击&#34;发布&#34;驱动程序中CoachPart参数的所有字段(即FirstName,LastName,Email)都为空。

我可以查看http请求,我可以看到我在表单上添加的值,但是他们没有将它发送到CoachPart。

为什么CoachPart字段无法归档?

谢谢!

1 个答案:

答案 0 :(得分:0)

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

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());
    }
}