Got an error when adding product property in plugin's model - nopcommerce

I want to show most viewed category wise product in category home page using plugin at public store
I made a new plugin name is:Nop.Plugin.MostViewProduct.Product
I added model folder and write code in it
that is below :
namespace Nop.Plugin.MostViewProduct.Product.Models
{
public partial class MostViewProductModel : BaseNopModel
{
[NopResourceDisplayName("MostViewProduct.ProductId")]
[AllowHtml]
public int ProductId { get; set; }
[NopResourceDisplayName("MostViewProduct.ProductCount")]
[AllowHtml]
public int ProductCount { get; set; }
public virtual Product Product { get; set; }
}
}
and I got error when add public virtual Product Product { get; set;} and error is Nop.Plugin.MostviewProduct.Product is a 'namespace' but is used like a typed .i has already include namespace that is using Nop.Core.Domain.Catalog;
why it is generating an error?
thank you in advance.

There is a conflict between the Nop.Plugin.MostViewProduct.Product namespace and the Nop.Core.Domain.Catalog.Product type. To resolve this, explicitly define which your property should use:
public virtual Nop.Core.Domain.Catalog.Product Product { get; set; }

Related

Implement one-to-many relationship in ASP.NET MVC 5

This is my project model :
public class Project
{
public int ProjectID { get; set; }
public string ProjectTitle { get; set; }
public string ProjectDetails { get; set; }
public virtual ICollection<Proposal> Proposals{ get; set; }
}
This is my Proposal model :
public class Proposal
{
public int ProposalID { get; set; }
public string BidTitle { get; set; }
public string BidDetails { get; set; }
public virtual Project Project { get; set; }
}
As you can see, there is one-to-many relationship between Project and Proposal. In
mydomain/Project/Details/ProjectID
view, I want to put a button, when this button is clicked, user can create a new Proposal for that project. My question is how I can pass that project's information to bid? If you can give me some tips about it, I'd be really glad. Thanks.
Create a model known as a viewmodel, which includes both the models you want to use under the same view. Your would look something like this:
public class ProposalAndProjectModel
{
public Proposal Proposal { get; set; }
public Project Project{ get; set; }
}
Save it as something like ProposalAndProjectModel.cs and then in your view, reference this model.
Now in your view you will be able to do the following:
Model.Proposal.propertyName
or
Model.Project.propertyName
This should help you as for getting the correct parameters for creating new objects.
You say when user click button user goto another page. You can sen projectID as get to that page. Thats how you can get that projectID.

Tree structure with reference to root in Entity Framework

I'm trying to model a tree structure for orders in Entity Framework. Right now I've go the following:
public class ProjectModel
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public int CustomerId { get; set; }
public virtual List<ProjectNode> Nodes { get; set; }
}
public class ProjectNode
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
public string Path { get; set; }
public int? ParentId { get; set; }
[ForeignKey("ParentId")]
public virtual List<ProjectNode> Children { get; set; }
}
What I need to be able to do is get a reference to the root ProjectModel at any level of ProjectNode in order to authorize a given user actually having permission to view and change the project which contains the ProjectNode.
public class ProjectNode {
public int ProjectId { get; set; } //<-- this
...
public class ProjectModel {
[Key]
public int Id { get; set; } //<-- containing the value of this
}
My question is whether its possible to have a theoretical ProjectId property populated at every level of the tree structure, or if I need to set it manually.
I had something working that at first blush appeared to allow this functionality, but upon further investigation only populated the ProjectId for ProjectNodes contained in the ProjectModel's Nodes property.
It seems to me like it would be super inefficient to recurse backwards through the structure to get to the root.
Credit due to #TestWell for this answer -
Apparently, all I needed to do for EF to automatically populate the ProjectId property on the ProjectNode was to change the name of the Id property in ProjectModel to ProjectId.
Unfortunately, this doesn't appear to work if I add a CustomerId property to the ProjectNode that I would like automatically populated from the property of the same name on the node's root ProjectModel, which I realized is the more efficient solution to what I'm trying to do.

MVC 4 Child Model Create and Edit views

I got 2 Models with 1-1 relationship.
public class CustomerModel
{
public int Id { get; set; }
public string Name { get; set; }
public AddressModel Address { get; set; }
}
public class AddressModel
{
public int Id { get; set; }
public string Street { get; set; }
}
Now I need a view that I can link the Address model with the customer, so, in the create of the customerModel, it bring the address create too, and linked, like in the post the address will be in the customer field.
#model Mvc.Models.CustomerModel
#Html.EditorForModel(Model)
#Html.EditorFor(x => x.Address)
If you create your view like that, you will then able to post back to an action result that takes a CustomerModel and the binding should work correctly
You'll probably want to do a bit more with custom annotations etc. as I doubt you'll want the user to be able to edit the Address id, but that should point you in the right direction

Referencing the creator of a page blog (the connected user) in my entity

I have an entity for my accessing my pages (pages of a blog).
Here it is:
public class Page
{
[Key]
public int PageId { get; set; }
public string AuthorName { get; set; }
[ForeignKey("AuthorName")]
public virtual MembershipUser Author { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string Content { get; set; }
public DateTime? PublishDate { get; set; }
public bool Published { get; set; }
public DateTime LastModified { get; set; }
}
As you can see, I would like to keep a reference to the person who created the page (in fact this is the connected user). So I defined an Author member in my class (of type MembershipUser). I also try to define the foreign key but it doesn't work because there is no primary key in the MembershipUser entity. I cannot add one to this entity because if is an aspnet object.
How can I proceed in order to have a reference to the creator of the page in my Page entity?
Thanks.
You can extend the MembershipUser by creating a CustomMembershipUser that will inherit from MembershipUser.
You can add any fields you want to your CustomMembershipUser, you will then also have to create a table of your own with both the original fields and your extra fields.
Here is some documentation that describes how you can do this.
What ORM are you using?
Anyway, you won't be able to reference a class that is not mapped in your ORM. You could create an entity like SystemUser, map it to a table and reference it at your Page entity. When you log in using Membership, you could query that SystemUser and store it in HttpSession so you can use it later.

EF Code first Eager loading problem

I have two entities in 1:n relationship: Category and Product.
public class Category
{
public int CategoryID { get; set; }
public string CategoryName { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
public class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public virtual Product { get; set; }
}
public class context : DbContext
{
public DbSet<Category> Categories { get; set; }
public DbSet<Product> Products { get; set; }
}
I want to load products in every category by Eager loading.
context.Categories.Include(c=>c.Products)
but Include do not load any navigation property. it accept only one parameter called "path" typed string.
Are you missing a using? VS 2010 is a bit dumb on this one and you often have to explicitly put in:
using System.Data.Entity
..to get the lambda include option available.
It won't prompt you to add it as it already has a string based definition for Include available under
System.Linq
.Include(x => x.MyObject) is actually a new extension method for the existing linq method.

Resources