2
votes

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?

3

3 Answers

2
votes

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.

1
votes

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

1
votes

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