Orchard 1.10.2 TryUpdateModel not working after upgrade from 1.7 - orchardcms

Newbie to Orchard CMS
After upgrading an existing Orchard site running version 1.7 to version 1.10.2, some (not all) of my content part drivers are no longer updating the part properties with the new values entered into the form on add or edit.
When I go into add or edit an item, the only property that is saved is an MediaPickerField that is a field of the part.
I have compared the non-working code with working content part drivers in the solution and I can't see anything that is different from them that would cause the issue.
TryUpdateModel is returning true, but none of the values on the form flow through. On Add, there will be a record in the DB with all empty fields except the Id. The driver returns the edit view with a message that the part was added successfully, but all the fields are cleared out except the Image field.
Here is the code for one of the parts having the issue.
public class FeaturedPromoPartRecord : ContentPartRecord {
[StringLength(100)]
public virtual string Name { get; set; }
[StringLengthMax]
public virtual string LinkUrl { get; set; }
public virtual FeaturedPromoGroupPartRecord FeaturedPromoGroupPartRecord { get; set; }
public virtual int SlideOrder { get; set; }
public virtual DateTime? StartDate { get; set; }
public virtual DateTime? EndDate { get; set; }
}
public class FeaturedPromoPart : ContentPart<FeaturedPromoPartRecord> {
public string Name
{
get { return Record.Name; }
set { Record.Name = value; }
}
public string LinkUrl {
get { return Record.LinkUrl; }
set { Record.LinkUrl = value; }
}
public FeaturedPromoGroupPartRecord FeaturedPromoGroupPartRecord
{
get { return Record.FeaturedPromoGroupPartRecord; }
set { Record.FeaturedPromoGroupPartRecord = value; }
}
public int SlideOrder {
get { return Record.SlideOrder; }
set { Record.SlideOrder = value; }
}
public DateTime? StartDate
{
get { return Record.StartDate; }
set { Record.StartDate = value; }
}
public DateTime? EndDate
{
get { return Record.EndDate; }
set { Record.EndDate = value; }
}
}
public class FeaturedPromoPartDriver : ContentPartDriver<FeaturedPromoPart> {
private readonly IFeaturedPromoService _featuredPromoService;
public FeaturedPromoPartDriver(IFeaturedPromoService featuredPromoService) {
_featuredPromoService = featuredPromoService;
T = NullLocalizer.Instance;
}
public Localizer T { get; set; }
protected override DriverResult Display(FeaturedPromoPart part, string displayType, dynamic shapeHelper) {
var group = _featuredPromoService.GetFeaturedPromoGroup(part.FeaturedPromoGroupPartRecord.Id);
return ContentShape("Parts_FeaturedPromo_SummaryAdmin",
() => shapeHelper.Parts_FeaturedPromo_SummaryAdmin(ContentPart: part, ContentItem: part.ContentItem, Group: group));
}
protected override DriverResult Editor(FeaturedPromoPart part, dynamic shapeHelper) {
var groups = _featuredPromoService.GetFeaturedPromoGroups();
var viewModel = new FeaturedPromoEditViewModel
{
Groups = groups,
Name = part.Name,
FeaturedPromoGroupPartRecordId = (part.FeaturedPromoGroupPartRecord == null) ? 0 : part.FeaturedPromoGroupPartRecord.Id,
LinkUrl = part.LinkUrl,
SlideOrder = part.SlideOrder,
StartDate = part.StartDate,
EndDate = part.EndDate
};
return ContentShape("Parts_FeaturedPromo_Edit",
() => shapeHelper.EditorTemplate(TemplateName: "Parts.FeaturedPromo.Edit", Model: viewModel));
}
protected override DriverResult Editor(FeaturedPromoPart part, IUpdateModel updater, dynamic shapeHelper) {
var result = updater.TryUpdateModel(part, Prefix, null, null);
var group = _featuredPromoService.GetFeaturedPromoGroup(part.FeaturedPromoGroupPartRecord.Id);
if(group != null)
part.FeaturedPromoGroupPartRecord = group._record.Value;
if (part.StartDate.HasValue && part.EndDate.HasValue && part.StartDate > part.EndDate)
updater.AddModelError("", T("The End Date cannot be after the Start Date."));
return Editor(part, shapeHelper);
}
}
Some images of what I'm seeing.
Edit
Add New
If anyone has encountered this issue, or has some insight on how to fix I would greatly appreciate your help!
Thank you

Related

Orchard CMS module no values from Editor

I've created my first simple module but for some reason my editor doesn't seem to be getting any values from the form.
The code is basically a slight modification on the Maps module example. (I've trimmed usings and whatnot for brevity.) The example is here: http://docs.orchardproject.net/Documentation/Writing-a-content-part
EDIT: I narrowed it down to my bool and DateTime properties. If I only use strings and ints my module works as expected. I have looked at other migrations in my modules directory and they use the generic Column method. I tried this, but still cannot get it to work.
Model:
namespace Maps.Models
{
public class MapRecord : ContentPartRecord
{
public virtual int SenderId { get; set; }
public virtual int RecipientId { get; set; }
public virtual string Subject { get; set; }
public virtual string Body { get; set; }
public virtual DateTime Timestamp { get; set; }
public virtual bool Read { get; set; }
public virtual int ReplyTo { get; set; }
}
public class MapPart : ContentPart<MapRecord>
{
[Required]
public int SenderId
{
get { return Record.SenderId; }
set { Record.SenderId = value; }
}
[Required]
public int RecipientId
{
get { return Record.RecipientId; }
set { Record.RecipientId = value; }
}
[Required]
public string Subject
{
get { return Record.Subject; }
set { Record.Subject = value; }
}
[Required]
public string Body
{
get { return Record.Body; }
set { Record.Body = value; }
}
[Required]
public DateTime Timestamp
{
get { return Record.Timestamp; }
set { Record.Timestamp = value; }
}
[Required]
public bool Read
{
get { return Record.Read; }
set { Record.Read = value; }
}
[Required]
public int ReplyTo
{
get { return Record.ReplyTo; }
set { Record.ReplyTo = value; }
}
}
}
Migrations.cs
public class Migrations : DataMigrationImpl {
public int Create() {
// Creating table MapRecord
SchemaBuilder.CreateTable("MapRecord", table => table
.ContentPartRecord()
.Column<int>("RecipientId")
.Column<int>("SenderId")
.Column<string>("Subject")
.Column<string>("Body")
.Column<DateTime>("Timestamp")
.Column<bool>("Read")
.Column<int>("ReplyTo")
);
ContentDefinitionManager.AlterPartDefinition(
typeof(MapPart).Name, cfg => cfg.Attachable());
return 1;
}
}
If this is similar to the example there could be two possible issues. 1) Maybe you haven't added the placement file. If the migration worked and there is no placement file then you won't see the data. 2) Another possible solution if the migration is failing would be that it doesn't have the correct DataTypes for bool and DateTime. Instead try DBType.Boolean and DBType.DateTime. Let me know if that works for you.

In Orchard how do I create a query that includes an associated Part?

I'm working in a new module for an orchard project. We've build a Company Part which is associated with an Address Part. What I'm eventually trying to get to is allowing users to enter various search criteria via the ui (non-admin interface), perform a search against all companies and return any matching results.
Where I'm struggling is how to build the query which allows me to include info from the associated address. So far I've only been able to successfully return results based on the CompanyPartRecord as opposed to the CompanyPart. The CompanyPartRecord obviously only contains the Address_Id of the associated AddressPart.
In essence I'm trying to get to something like this which will return an enumerable or list of companies which match the entered search criteria. There are more search options beyond zip so I'm looking for an extensible solution.
var query = _companyRepository.Table.AsQueryable();
if (!string.IsNullOrEmpty(Zip))
query = query.Where(c => c.Address.Zip == Zip);
var queryResult = query.ToList();
I've tried using both an IRepository (shown above) and with IContentManager such as the following:
var query = _contentManager.Query<CompanyPart, CompanyPartRecord>();
//var query = _contentManager.Query<CompanyPart>();
if (!string.IsNullOrEmpty(Zip))
query = query.Where(c => c.Address.Zip == Zip);
var queryResult = query.ToList();
Here's the actual Company and Address (Parts and Records):
public class CompanyPartRecord : ContentPartRecord
{
public virtual string Name { get; set; }
public virtual string Description { get; set; }
public virtual int Address_Id { get; set; }
public virtual string Phone { get; set; }
public virtual string OtherInformation { get; set; }
public virtual string Website { get; set; }
public virtual bool Inactive { get; set; }
// Keywords - Taxonomy
// Logo - associated part
// HasMissingData - derived
// NumberOfEmployees - associated part
}
public class CompanyPart : ContentPart<CompanyPartRecord>, ITitleAspect
{
internal readonly LazyField<AddressPart> AddressField = new LazyField<AddressPart>();
public string Name
{
get { return Record.Name; }
set { Record.Name = value; }
}
public string Description
{
get { return Record.Description; }
set { Record.Description = value; }
}
public AddressPart Address
{
get { return AddressField.Value; }
set { AddressField.Value = value; }
}
public string Phone
{
get { return Record.Phone; }
set { Record.Phone = value; }
}
public string OtherInformation
{
get { return Record.OtherInformation; }
set { Record.OtherInformation = value; }
}
public string Website
{
get { return Record.Website; }
set { Record.Website = value; }
}
public bool Inactive
{
get { return Record.Inactive; }
set { Record.Inactive = value; }
}
public string Title
{
get { return Name; }
}
}
public class AddressPartRecord : ContentPartRecord
{
public virtual string Street { get; set; }
public virtual string Street2 { get; set; }
public virtual string City { get; set; }
public virtual string CountyName { get; set; }
public virtual NationRecord State { get; set; }
public virtual string Zip { get; set; }
}
public class AddressPart : ContentPart<AddressPartRecord>
{
public string Street
{
get { return Record.Street; }
set { Record.Street = value; }
}
public string Street2
{
get { return Record.Street2; }
set { Record.Street2 = value; }
}
public string City
{
get { return Record.City; }
set { Record.City = value; }
}
public string CountyName
{
get { return Record.CountyName; }
set { Record.CountyName = value; }
}
public NationRecord State
{
get { return Record.State; }
set { Record.State = value; }
}
public string Zip
{
get { return Record.Zip; }
set { Record.Zip = value; }
}
}
I assume that you have a Content Type that contains both Parts (ie. named Company), if that is the case this should work:
var query = _contentManager.Query("Company").Where<AddressPartRecord>(x => x.Street == "query street");
var list = query.ToList();
The list contains the ContentItems filtered by street. Using .As() and .As() over the items of the list you get access to each part.

Orchard Content Type is null

i am new in orchard module development.i create a module.when i try to save data.
i use this code fore save data
public ActionResult Create(FormCollection input)
{
var product = contentManager.New<ProductPart>("Product");
product.EmployeeName = input["EmployeeName"];
product.EmployeeFathersName = input["EmployeeFathersName"];
product.DOB = Convert.ToDateTime(input["DOB"]);
product.Email = input["Email"];
product.Address = input["Address"];
product.JoiningDate = Convert.ToDateTime(input["JoiningDate"]);
if (!ModelState.IsValid)
{
return View(product);
}
contentManager.Create(product);
return RedirectToAction("Index");
}
this class i use in Model
public class ProductRecord:ContentPartRecord
{
public virtual string EmployeeName { get; set; }
public virtual string EmployeeFathersName { get; set; }
public virtual DateTime DOB { get; set; }
public virtual string Email { get; set; }
public virtual string Address { get; set; }
public virtual DateTime JoiningDate { get; set; }
}
public class ProductPart : ContentPart<ProductRecord>
{
/*
public int Id
{
get { return Record.Id; }
set{Record.Id = value;}
}
*/
[Required]
public string EmployeeName
{
get { return Record.EmployeeName; }
set { Record.EmployeeName = value; }
}
[Required]
public string EmployeeFathersName
{
get { return Record.EmployeeFathersName; }
set { Record.EmployeeFathersName = value; }
}
[Required]
public DateTime DOB
{
get { return Record.DOB; }
set { Record.DOB = value; }
}
[Required]
public string Email
{
get { return Record.Email; }
set { Record.Email = value; }
}
[Required]
public string Address
{
get { return Record.Address; }
set { Record.Address = value; }
}
[Required]
public DateTime JoiningDate
{
get { return Record.JoiningDate;}
set { Record.JoiningDate = value; }
}
}
i use content type "Product" but when it goes orchard ContentCreateExtension in belows method
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;
}
here i face var part is null that means it content part is null.
please help me....
Have you setup your migrations class?
i.e.
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")
);
Also have you setup your repository?
i.e.
public class ProductPartHandler : ContentHandler {
public ProductPartHandler(IRepository<ProductPartRecord> repository) {
Filters.Add(StorageFilter.For(repository));
}
In addition to the Nicholas answer, I want to mention, that missing driver for the ProductPart can cause such error. Make sure, that you have at least empty driver defined.
public class ProductPartDriver : ContentPartDriver<ProductPart> {}
Just went through a similar situation, be sure that the handler class is declared as public.

How to save data through Orchard module?

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?
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.
I have mentioned this in you other thread.. Orchard Content Type is null
you need
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")
);
Repository
public class ProductPartHandler : ContentHandler {
public ProductPartHandler(IRepository repository) {
Filters.Add(StorageFilter.For(repository));
}
Hope this helps
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

ValueInjecter question

After working with AutoMapper I came across ValueInjecter on this site. I am trying it out but I am stuck on what is probably a very simple scenario.
But before I dig into the code sample, does anyone know if ValueInjecter works in a Medium-Trust web environment? (Like Godaddy?)
Ok, onto the code! I have the following models:
public class NameComponent
{
public string First { get; set; }
public string Last { get; set; }
public string MiddleInitial { get; set; }
}
public class Person
{
public NameComponent Name { get; set; }
}
that I want to map to the following DTO:
public class PersonDTO : BaseDTO
{
private string _firstName;
public string FirstName
{
get { return _firstName; }
set { NotifyPropertyChanged(() => FirstName, ref _firstName, value); }
}
private string _middleInitial;
public string MiddleInitial
{
get { return _middleInitial; }
set { NotifyPropertyChanged(() => MiddleInitial, ref _middleInitial, value); }
}
private string _lastName;
public string LastName
{
get { return _lastName; }
set { NotifyPropertyChanged(() => LastName, ref _lastName, value); }
}
}
So when I want to Map from Model to DTO I need a Model.Name.First -> DTO.FirstName
and when going from DTO to Model I need FirstName -> Name.First. From my understanding this is not a simple Flatten/UnFlatten, because the words also reverse themselves, ie: FirstName <--> Name.First. So First and Last names could use the same kind of rule, but what about MiddleInitial? Model.Name.MiddleInitial -> DTO.MiddleInitial.
I see there are some plugins, but none of them seem to do what I would want. Has anyone else come across this scenario?
the basic idea is that I match the Name with the FirstName, I take this as a pivot point, and in the method that usually sets the value to just one (FirstName) property I set it to 3 properties - that's for the FromNameComp
in the ToNameComp i match the same properties but I take the value from 3 and create one and set it
public class SimpleTest
{
[Test]
public void Testit()
{
var p = new Person { Name = new NameComponent { First = "first", Last = "last", MiddleInitial = "midd" } };
var dto = new PersonDTO();
dto.InjectFrom<FromNameComp>(p);
Assert.AreEqual(p.Name.First, dto.FirstName);
Assert.AreEqual(p.Name.Last, dto.LastName);
Assert.AreEqual(p.Name.MiddleInitial, dto.MiddleInitial);
var pp = new Person();
pp.InjectFrom<ToNameComponent>(dto);
Assert.AreEqual(dto.LastName, pp.Name.Last);
Assert.AreEqual(dto.FirstName, pp.Name.First);
Assert.AreEqual(dto.MiddleInitial, pp.Name.MiddleInitial);
}
public class FromNameComp : ConventionInjection
{
protected override bool Match(ConventionInfo c)
{
return c.SourceProp.Name == "Name" && c.SourceProp.Type == typeof(NameComponent)
&& c.TargetProp.Name == "FirstName"
&& c.SourceProp.Value != null;
}
protected override object SetValue(ConventionInfo c)
{
dynamic d = c.Target.Value;
var nc = (NameComponent)c.SourceProp.Value;
//d.FirstName = nc.First; return nc.First does this
d.LastName = nc.Last;
d.MiddleInitial = nc.MiddleInitial;
return nc.First;
}
}
public class ToNameComponent : ConventionInjection
{
protected override bool Match(ConventionInfo c)
{
return c.TargetProp.Name == "Name" && c.TargetProp.Type == typeof(NameComponent)
&& c.SourceProp.Name == "FirstName";
}
protected override object SetValue(ConventionInfo c)
{
dynamic d = c.Source.Value;
var nc = new NameComponent { First = d.FirstName, Last = d.LastName, MiddleInitial = d.MiddleInitial };
return nc;
}
}
public class NameComponent
{
public string First { get; set; }
public string Last { get; set; }
public string MiddleInitial { get; set; }
}
public class Person
{
public NameComponent Name { get; set; }
}
public class PersonDTO
{
public string FirstName { get; set; }
public string MiddleInitial { get; set; }
public string LastName { get; set; }
}
}
But before I dig into the code sample,
does anyone know if ValueInjecter
works in a Medium-Trust web
environment? (Like Godaddy?)
it doesn't use reflection.emit so it should work

Resources