I. Create part
1: The project references orchard. Framework;
2: Create a models folder;
3: Create the productpartrecord class in the models folder, as shown below:
Public class productpartrecord: contentpartrecord
{
Public Virtual decimal unitprice {Get; set ;}
Public Virtual string SKU {Get; set ;}
}
Note: It is virtual, because the nhib.pdf of orchard needs this.
And productrecord:
Public class productpart: contentpart <productpartrecord>
{
Public decimal unitprice
{
Get {return record. unitprice ;}
Set {record. unitprice = value ;}
}
Public String SKU
{
Get {return record. SKU ;}
Set {record. SKU = value ;}
}
}
Ii. update the database
Updating the database depends on a type called Migrations. We need to create it in the root directory, as shown below:
Public class migrations: datamigrationimpl
{
Public int create ()
{
Schemabuilder. createtable ("productpartrecord", table => table
// The following method will create an "ID" column for us and set it is the primary key for the table
. Contentpartrecord ()
// Create a column named "unitprice" of type "decimal"
. Column <decimal> ("unitprice ")
// Create the "SKU" column and specify a maximum length of 50 characters
. Column <string> ("SKU", column => column. withlength (50 ))
);
// Return the version that this feature will be after this method completes
Return 1;
}
}
If you do not know how to use migrations, refer to orchard's module upgrade. Now, we query the database:
The Code has been executed, and the table tminji_shop_productpartrecord has been created.
3. Create productpart
We have created the productpart class, but it is not enough. We need to update this classContentpartdefinitionIn this table, you must specify that the productpart is attachable, that is, you must attach the productpart in the background.
Now, we continue to do this through code, and we still do it in migrations. This time, we first need to introduce orchard. Core and then add the following code:
Compile, refresh the background, and view the database:
It is found that the version is 2 and the settings_contentpartdefinitionrecord table has more row data:
And enter the background
More found:
Edit:
This is exactly what our code defines.
4. Add driver
Now, content-> content types andCreate new type,Name book and select:Body,Comments,Product,Title, Autoroute, AndTags, Save, and then, repeat the above to add a DVD.
At this point, if we add a book, we will find that we do not see the price and SKU. Where are they? Yes, we still lack a driver,
The driver is similar to the MVC controller, but it is responsible for the contentpart. Typically, there are three methods: one for the front-end display of the part and the other for the back-end editing mode, A part is used to process the content item stored by the user (this time, the part is attached ).
The returned value is driverresult. Of course, in most cases, it is actually returned as shaperesult (inherited from driverresult ). Shaperesult tells orchard how the razor template render part.
Now, create it:
1: first, create the DRIVERS directory;
2: Create productpartdriver:
Using orchard. contentmanagement;
Using orchard. contentmanagement. drivers;
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. Threading. tasks;
Using tminji. Shop. models;
Namespace tminji. Shop. Drivers
{
Public class productpartdriver: contentpartdriver <productpart>
{
Protected override string prefix
{
Get {return "product ";}
}
Protected override driverresult Editor (productpart part, dynamic shapehelper)
{
Return contentshape ("parts_product_edit", () => shapehelper
. Editortemplate (templatename: "parts/product", model: part, Prefix: prefix ));
}
Protected override driverresult Editor (productpart part, iupdatemodel Updater, dynamic shapehelper)
{
Updater. tryupdatemodel (part, prefix, null, null );
Return Editor (part, shapehelper );
}
}
}
Now it is necessary to describe the Code:
1: The current driver has two methods. One is used by the producer when the editor of productpart is displayed, and the other is called when the editor form is submitted in the background (the Updater method is included ).
2: We name our shape parts_product_edit. In this way, its view is views/editortemplates/parts/product. cshtml.
Now, you can add this view.
@ Using system. Web. MVC. html
@ Model tminji. Shop. Models. productpart
<Fieldset>
<Legend> product fields </legend>
<Div class = "editor-label"> @ html. labelfor (x => X. SKU) </div>
<Div class = "editor-field">
@ Html. editorfor (x => X. SKU)
@ Html. validationmessagefor (x => X. SKU)
</Div>
<Div class = "hint"> enter the stock keeping Unit </div>
<Div class = "editor-label"> @ html. labelfor (x => X. unitprice) </div>
<Div class = "editor-field">
@ Html. editorfor (x => X. unitprice)
@ Html. validationmessagefor (x => X. unitprice)
</Div>
<Div class = "hint"> enter the sales price per unit </div>
</Fieldset>
To make this view correctly rendered, we also need to add references.
System. Web
System. Web. MVC
System. Web. webpages
The former is under the global assembly, and the latter two can be found under Lib \ aspnetmvc (Note: You must know which LIB This Lib is ).
Then, there is still a web. config in our root directory. I suggest you copy it directly from the blog module (Note: My orchard is version 1.8 ):
<? XML version = "1.0"?>
<Configuration>
<Configsections>
<Sectiongroup name = "system. web. webpages. razor "type =" system. web. webpages. razor. configuration. razorwebsectiongroup, system. web. webpages. razor, version = 3.0.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35 ">
<Remove name = "host"/>
<Remove name = "pages"/>
<Section name = "host" type = "system. web. webpages. razor. configuration. hostsection, system. web. webpages. razor, version = 3.0.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35 "requirepermission =" false "/>
<Section name = "pages" type = "system. web. webpages. razor. configuration. razorpagessection, system. web. webpages. razor, version = 3.0.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35 "requirepermission =" false "/>
</Sectiongroup>
</Configsections>
<System. Web. webpages. Razor>
<Host factorytype = "system. Web. MVC. mvcwebrazorhostfactory, system. Web. MVC, version = 5.1.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35"/>
<Pages pagebasetype = "orchard. MVC. viewengines. Razor. webviewpage">
<Namespaces>
<Add namespace = "system. Web. MVC"/>
<Add namespace = "system. Web. MVC. Ajax"/>
<Add namespace = "system. Web. MVC. html"/>
<Add namespace = "system. Web. Routing"/>
<Add namespace = "system. Web. webpages"/>
<Add namespace = "system. LINQ"/>
<Add namespace = "system. Collections. Generic"/>
<Add namespace = "orchard. MVC. html"/>
</Namespaces>
</Pages>
</System. Web. webpages. Razor>
<System. Web>
<Compilation targetframework = "4.5">
<Assemblies>
<Add Assembly = "system. Web. Export actions, version = 4.0.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35"/>
<Add Assembly = "system. Web. Routing, version = 4.0.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35"/>
<Add Assembly = "system. Data. LINQ, version = 4.0.0.0, culture = neutral, publickeytoken = b77a5c561934e089"/>
<Add Assembly = "system. Web. MVC, version = 5.1.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35"/>
<Add Assembly = "system. Web. webpages, version = 3.0.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35"/>
</Assemblies>
</Compilation>
</System. Web>
</Configuration>
Then, in the root directory, create a placement.info,
<Placement>
<Place parts_product_edit = "content: 1"/>
</Placement>
This is to tell orchard, please explicitly in the content area of the widget.
Now we can see:
5. Add handler (persistent data)
Let's add the following data:
Books:
- The Hobbit, $50, SKU-1001
- Wizard's first rule, $39, SKU-1002
- The Hunger Games, $29, SKU-1003
DVDs:
- Prometheus, $30, SKU-1004
- The Shawshank Redemption, $25, SKU-1005
- The Dark Knight, $20, SKU-1006
Then, the data is saved successfully. Then, we plan to modify the data and find that the price and SKU are empty. Why? Because we have not implemented persistence. To make orchard persistent data, we need:
1: CreateHandlersFolder;
2: addProductparthandler, As follows:
Public class productparthandler: contenthandler
{
Public productparthandler (irepository <productpartrecord> repository)
{
Filters. Add (storagefilter. For (repository ));
}
}
Now we can see that we can add or delete data. Check the database: