Reputation: 4296
Models:
ManufacturersModel.cs
public class Manufacturers
{
[Key]
public int ManufacturerID { get; set; }
[Required]
[StringLength(100)]
public string Name { get; set; }
public int AddressID { get; set; }
public Address Address { get; set; }
[EmailAddress]
[StringLength(150)]
public string EmailAddress { get; set; }
[Phone]
[StringLength(20)]
public string PhoneNumber { get; set; }
public ICollection<Brands> Brands { get; set; }
}
BrandsModel.cs
public class Brands
{
[Key]
public int BrandID { get; set; }
[Required]
[StringLength(100)]
public string BrandName { get; set; }
[Required]
public int ManufacturerID { get; set; }
public Manufacturers Manufacturers { get; set; }
public ICollection<Products> Products { get; set; }
}
So when trying to create a "Create" page for Brands, I want to have two fields: Brand Name and Manufacturer Name. The Brand Name would be a text box/editor that the user would enter in what they want. I want the Manufacturer Name, because of dependencies, to be an jQuery UI autocomplete drop down, so that I know the dependency will be met. However, I'm not sure if my @model should be Brands or Manufacturers.
This extends to other pages I'll likely work on that may require more than one model. I have a context that contains all of the models that I've created, but not sure if I can/should send the context to the page, or just model(s). Another page, as an example, that I'll want to create is a Create Product page which would have a Manufacturer Name jQuery UI autocomplete drop down as well as a Brand Name jQuery UI autocomplete drop down as Products relate to the brand the same as the brand relates to the Manufacturer.
Just getting into Code First development, so need all the help and tips I can get. Thanks you very much.
Upvotes: 2
Views: 258
Reputation: 16991
These classes look like a data model. You probably don't want to use them as the actual model for your page (sometimes called a view model). You should look at creating a model specifically for your page that has properties on it that represent that data needed to render that page. This model won't be stored in the database, it is a model used for the application only.
In your case, for this page, I would create a model that has a string property for the typed brand name, and an integer property for the selected manufacturer it. You may also want to consider adding a property that is a list of key value pairs to act as the data source of the manufacturer selection, though you could also get that data via ajax on an alternate action method of the controller if you want to get fancy.
The controller should be responsible for creating an populating this view model with information from the data model, and will also do the reverse when it is saved.
This seems more like a question about MVC than Entity Framework.
Upvotes: 1