如何通过Orchard模块保存数据?

发布于 2024-11-29 04:38:30 字数 1980 浏览 1 评论 0原文

我是果园新手。

为了学习 orchard 模块开发,我正在按照文档尝试创建一个商务模块。

该模块由产品部分和产品类型组成,其中产品部分有产品部分。

当我尝试使用以下方法保存数据时:

public ActionResult Create(FormCollection input)
    {
        var product = contentManager.New<ProductPart>("Product");
        product.Description = input["Description"];
        product.Sku = input["Sku"];
        product.Price =Convert.ToDecimal(input["Price"]);

        if (!ModelState.IsValid)
        {
            return View(product);
        }

        contentManager.Create(product);

        return RedirectToAction("Index");
    }    

我收到错误,指出特定转换无效且 part(ContentPart) 为 null。

public static T New<T>(this IContentManager manager, string contentType)
 where T : class, IContent {
        var contentItem = manager.New(contentType);
        if (contentItem == null)
            return null;

        var part = contentItem.Get<T>();
        if (part == null)
            throw new InvalidCastException();

        return part;
    }

我使用内容类型 Product 并且我有 ProductRecord 类用于存储数据,如下所示:

public class ProductRecord:ContentPartRecord
{
   // public virtual int Id { get; set; }


    public virtual string Sku { get; set; }


    public virtual string Description { get; set; }


    public virtual decimal Price { get; set; }


}



public class ProductPart : ContentPart<ProductRecord>
{
    /*
    public int Id
    {
        get { return Record.Id; }
        set{Record.Id = value;}
    }
    */

    [Required]
    public  string Sku
    {
        get { return Record.Sku; }
        set { Record.Sku = value; }
    }

    [Required]
    public  string Description 
    {
        get { return Record.Description; }
        set{ Record.Description = value;}
    }

    [Required]
    public  decimal Price 
    {
        get { return Record.Price; }
        set { Record.Price = value; }
    }


}

谁能告诉我我的问题是什么?

I'm new with orchard.

To learn orchard module development, I am following documentation to try to create a commerce module.

The module consists of product part and product type, which has product part.

When I try to save data in following method:

public ActionResult Create(FormCollection input)
    {
        var product = contentManager.New<ProductPart>("Product");
        product.Description = input["Description"];
        product.Sku = input["Sku"];
        product.Price =Convert.ToDecimal(input["Price"]);

        if (!ModelState.IsValid)
        {
            return View(product);
        }

        contentManager.Create(product);

        return RedirectToAction("Index");
    }    

I am getting an error that specific cast is Invalid and part(ContentPart) is null.

public static T New<T>(this IContentManager manager, string contentType)
 where T : class, IContent {
        var contentItem = manager.New(contentType);
        if (contentItem == null)
            return null;

        var part = contentItem.Get<T>();
        if (part == null)
            throw new InvalidCastException();

        return part;
    }

I used content type Product and I have ProductRecord class for storage data, as below:

public class ProductRecord:ContentPartRecord
{
   // public virtual int Id { get; set; }


    public virtual string Sku { get; set; }


    public virtual string Description { get; set; }


    public virtual decimal Price { get; set; }


}



public class ProductPart : ContentPart<ProductRecord>
{
    /*
    public int Id
    {
        get { return Record.Id; }
        set{Record.Id = value;}
    }
    */

    [Required]
    public  string Sku
    {
        get { return Record.Sku; }
        set { Record.Sku = value; }
    }

    [Required]
    public  string Description 
    {
        get { return Record.Description; }
        set{ Record.Description = value;}
    }

    [Required]
    public  decimal Price 
    {
        get { return Record.Price; }
        set { Record.Price = value; }
    }


}

Can anybody tell me what my problem is?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

新雨望断虹 2024-12-06 04:38:30

我只是猜测,但是您是否在migration.cs中声明了您的记录和ContentType?如果您不这样做,内容管理将无法使用您的类型创建内容项,因为它不知道有问题的类型。

你的migration.cs应该看起来像这样:

public class Migrations : DataMigrationImpl
{

    public int Create()
    {
        SchemaBuilder.CreateTable("ProductRecord",
            table =>
            {
                table.ContentPartRecord()
                     .Column<string>("Sku")
                     .Column<string>("Description")
                     .column<decimal>("Price");
            });            

        ContentDefinitionManager.AlterTypeDefinition("Product", cfg => cfg.WithPart("ProductPart"));

        return 1;
    }
}

顺便说一句,Orchard中的命名约定是将部件的记录命名为XXXPartRecord。但我认为你的问题不在于那里。

I'm just guessing, but did you declare your record and your ContentType in migration.cs? If you didn't, the content management will be unable to create a content item with your type as it will not know the type in question.

Your migration.cs should look somehow like that:

public class Migrations : DataMigrationImpl
{

    public int Create()
    {
        SchemaBuilder.CreateTable("ProductRecord",
            table =>
            {
                table.ContentPartRecord()
                     .Column<string>("Sku")
                     .Column<string>("Description")
                     .column<decimal>("Price");
            });            

        ContentDefinitionManager.AlterTypeDefinition("Product", cfg => cfg.WithPart("ProductPart"));

        return 1;
    }
}

On a side note, the naming convention in Orchard is to name the record for a part XXXPartRecord. I don't think your problem lies there though.

诠释孤独 2024-12-06 04:38:30

我在其他线程中提到过这一点。 Orchard 内容类型为 null

你需要

  1. 迁移

    公共类迁移:DataMigrationImpl {

    public int Create() {
        SchemaBuilder.CreateTable("产品记录",
            表=>桌子
                .ContentPartRecord()
                .需要指定列
            );
    
        ContentDefinitionManager.AlterTypeDefinition("论坛",
            cfg =>; cfg
                .WithPart("产品部分")
                .WithPart("公共部分")
            );
    
  2. 存储库

    公共类ProductPartHandler:ContentHandler {
    公共 ProductPartHandler(IRepository 存储库){
    Filters.Add(StorageFilter.For(存储库));
    }

这有帮助

I have mentioned this in you other thread.. Orchard Content Type is null

you need

  1. Migrations

    public class Migrations : DataMigrationImpl {

    public int Create() {
        SchemaBuilder.CreateTable("ProductRecord",
            table => table
                .ContentPartRecord()
                .COLUMNS NEED TO BE SPECIFIED
            );
    
        ContentDefinitionManager.AlterTypeDefinition("Forum",
            cfg => cfg
                .WithPart("ProductPart")
                .WithPart("CommonPart")
            );
    
  2. Repository

    public class ProductPartHandler : ContentHandler {
    public ProductPartHandler(IRepository repository) {
    Filters.Add(StorageFilter.For(repository));
    }

Hope this helps

墨小沫ゞ 2024-12-06 04:38:30

您可以尝试使用 pszmyd 的命令行实用程序生成类似的部分,看看有什么不同。

http://www.szmyd.com.pl /blog/通过命令行生成-orchard-content-parts-

You could try generating a similar part using the command line utility by pszmyd and see whats different.

http://www.szmyd.com.pl/blog/generating-orchard-content-parts-via-command-line

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文