Advice on datamodel structure - asp.net-mvc

I'm practicing with ASP.Net MVC 5, and I'm attempting to build a prototype events directory. I've started with a basic layout, adapted from the Contoso University tutorial, but I want to start to develop a more complex data model, however I'm unsure as to the best way to develop the model, specifically, the different pricing options each listing can have.
My current data model is;
Vendors
public class Vendor
{
public int ID { get; set; }
[Required]
[Display(Name = "Company Name")]
[StringLength(50, ErrorMessage = "Company name cannot be longer than 50 characters.")]
public string CompanyName { get; set; }
[Required]
[Display(Name = "First Name")]
[StringLength(50, ErrorMessage = "First name cannot be longer than 50 characters.")]
public string ContactFirstName { get; set; }
[Required]
[Display(Name = "Last Name")]
[StringLength(50, ErrorMessage = "Last name cannot be longer than 50 characters.")]
public string ContactLastName { get; set; }
[Required]
[Display(Name = "Address")]
public string Address { get; set; }
[Required]
[Display(Name = "Address Line 2")]
public string AddressLine2 { get; set; }
[Required]
public string Region { get; set; }
[Required]
public string City { get; set; }
[Required]
public string Postcode { get; set; }
[Required]
[Display(Name = "Email Address")]
public string Email { get; set; }
[Required]
[Display(Name = "Phone Number")]
public string Phone { get; set; }
[Display(Name = "Twitter #username")]
public string TwitterHandle { get; set; }
[Display(Name = "Facebook URL")]
public string FacebookURL { get; set; }
[Required]
[Display(Name = "Active Vendor?")]
public Boolean ActiveStatus { get; set; }
public virtual ICollection<Listing> Listings { get; set; }
}
Listings
public class Listing
{
public int ListingID { get; set; }
public int CategoryID { get; set; }
public int VendorID { get; set; }
[Required]
[Display(Name = "Listing Name")]
public string ListingName { get; set; }
[Required]
[Display(Name = "Listing Description")]
public string ListingDesc { get; set; }
[Required]
[Display(Name = "Maximum Capacity")]
public int Capacity { get; set; }
[Required]
[DataType(DataType.Currency)]
[Display(Name = "Price Per Guest")]
public decimal PricePerGuest { get; set; }
[Required]
public string Address { get; set; }
[Display(Name = "Address Line 2")]
public string Address2 { get; set; }
[Required]
public string City { get; set; }
[Required]
public string Postcode { get; set; }
public virtual Category Category { get; set; }
public virtual Vendor Vendor { get; set; }
}
Categories
public class Category
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int CategoryID { get; set; }
public string CategoryName { get; set; }
public string CategoryDescription { get; set; }
public virtual ICollection<Listing> Listings { get; set; }
}
Ideally, I'd like to add to the Listing table a new property called 'Fixed Price', giving vendors the option to either list by price per guest or by fixed price, as not all vendors can price their services by price per guest.
Is a separate table with pricing options sufficient enough to develop this further?
P.S - I haven't yet started to format the entities properly yet, hence why categories is missing validation and so on.

Price (currency) and IsFixedPrice (bit) Instead of PricePerGuest
Thanks to #Nikhil Vartak for the answer

Related

The country drop down list for agent model is not being created

I want to add a country drop down list to my agent form, the database part has been created when running the migration but when I generate the controllers and the views, the drop down list won't generate
public class Agent
{
public int AgentID { get; set; }
[Required]
[Display(Name = "Social Insurance Number")]
public string SIN { get; set; }
public Country Country { get; set; }
[Required]
[Display(Name = "First Name")]
[StringLength(20, ErrorMessage = "First Name cannot be longer than 20 characters")]
public string FirstName { get; set; }
}
public class Country
{
public int ID { get; set; }
[Required]
[Display(Name = "Country")]
public countries Name { get; set; }
public ICollection<Agent> Agents { get; set; }
}
public enum countries
{
canada,
usa
}

MVC Entity Framework Models and Relationships

im quite new to ASP.net and entity framework. I created a class but im just going to simplify it and take the example class from Microsoft. So here we go. These are all the example classes
public class Student
{
public int ID { get; set; }
public string LastName { get; set; }
public string FirstMidName { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime EnrollmentDate { get; set; }
public virtual ICollection<Enrollment> Enrollments { get; set; }
}
public class Instructor
{
public int ID { get; set; }
[Required]
[Display(Name = "Last Name")]
[StringLength(50)]
public string LastName { get; set; }
[Required]
[Column("FirstName")]
[Display(Name = "First Name")]
[StringLength(50)]
public string FirstMidName { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
[Display(Name = "Hire Date")]
public DateTime HireDate { get; set; }
[Display(Name = "Full Name")]
public string FullName
{
get { return LastName + ", " + FirstMidName; }
}
public virtual ICollection<Course> Courses { get; set; }
public virtual OfficeAssignment OfficeAssignment { get; set; }
}
public class OfficeAssignment
{
[Key]
[ForeignKey("Instructor")]
public int InstructorID { get; set; }
[StringLength(50)]
[Display(Name = "Office Location")]
public string Location { get; set; }
public virtual Instructor Instructor { get; set; }
}
public class Course
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
[Display(Name = "Number")]
public int CourseID { get; set; }
[StringLength(50, MinimumLength = 3)]
public string Title { get; set; }
[Range(0, 5)]
public int Credits { get; set; }
public int DepartmentID { get; set; }
public virtual Department Department { get; set; }
public virtual ICollection<Enrollment> Enrollments { get; set; }
public virtual ICollection<Instructor> Instructors { get; set; }
}
Like i said this is just for a example im trying to get know it.
My First Question: Do you need something like DepartmentID When you already do public virtual Department Department?
Second question: When i have a Collection of data how do i add values or Ids to that collection in the controller i can find this anywhere.
Third Question: Can some one link me a complete MVC example application with proper relationships

Save data finally after all action completed

hi please help me I have 5 model and I want to save all model at once so please help me
my models are
public partial class EmployeeMainTable
{
public EmployeeMainTable()
{
this.employee_DepartmentTable = new List<EmployeeDepartmentTable>();
}
public int EmployeeId { get; set; }
[Required(ErrorMessage = "Enter the Name")]
public string EmployeeName { get; set; }
[Required(ErrorMessage = "Enter date of joining")]
[DataType(DataType.Date)]
public System.DateTime EmployeeDateOfJoining { get; set; }
[Required(ErrorMessage = "Department Required")]
public int EmployeeDepartmentId { get; set; }
[Required(ErrorMessage = "Designation Required")]
public int EmployeeDesignationId { get; set; }
[Required(ErrorMessage = "Location Required")]
public int EmployeeLocationId { get; set; }
[Required(ErrorMessage = "Employee status Required")]
public int EmployeeStatusId { get; set; }
[Required(ErrorMessage = "Employee Type Required")]
public int EmployeeTypeId { get; set; }
[Required]
public bool EmployeeIsActive { get; set; }
[Required(ErrorMessage = "Confirmed Date Required")]
[DataType(DataType.Date)]
public DateTime EmployeeConfirmDate { get; set; }
[Required(ErrorMessage = " Email required")]
[RegularExpression(#"\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessage = " Must be a valid e-mail address ")]
public string EmployeeEmailId { get; set; }
[Required(ErrorMessage = "Contact Number Required")]
public string EmployeeContactNumber { get; set; }
[Required(ErrorMessage = "Date of Birth Required")]
[DataType(DataType.Date)]
public DateTime EmployeeDateOfBirth { get; set; }
public ICollection<EmployeeAssetsTable> employee_AssetsTable { get; set; }
public List<EmployeeDepartmentTable> employee_DepartmentTable { get; set; }
public ICollection<EmployeeDesignationTable> employee_DesignationTable { get; set; }
public ICollection<EmployeeEducationDetailTable> employee_EducationTable { get; set; }
public ICollection<EmployeeFamilyTable> employee_FamilyTable { get; set; }
public ICollection<EmployeeLocationTable> employee_LocationTable { get; set; }
public ICollection<EmployeeStatusTable> employee_StatusTable { get; set; }
public ICollection<EmployeeTypeTable> employee_TypeTable { get; set; }
public virtual ICollection<EmployeePreviousCompanyDetailTable> employee_PreviousCompanyDetailTable { get; set; }
public virtual ICollection<EmployeeDocumentsTable> employee_DocumentTable { get; set; }
likewise I have 5 models
what I need I need to pas model from action to action and if I click submit button in last section data get saved from all model at once
If I understand correctly, can't you just have a property in model #2 for model#1, a property for model #2 and #1 in model #3, and so forth..
Or use custom ViewModels that's served to fit your purpose.
I just started with MVC so this is my novice advice, don't take it for granted.

Excluding properties from a viewmodel

I have a viewmodel that contains a CustomerModel e.g.
public class MyAccountViewModel
{
public CustomerModel Customer { get; set; }
public LoginModel Login { get; set; }
public ICollection<AuthenticationClientData> Clients { get; set; }
public bool HasLocalPassword { get; set; }
public LocalPasswordModel Password { get; set; }
}
[DataContract]
public class CustomerModel
{
[DataMember]
public Guid CustomerBusinessId { get; set; }
[DataMember(IsRequired = true)]
[Required(ErrorMessage = "First Name is required")]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[DataMember(IsRequired = true)]
[Required(ErrorMessage = "Last Name is required")]
[Display(Name = "Last Name")]
public string LastName { get; set; }
[DataMember]
public string FullName
{
get { return string.Format("{0} {1}", FirstName, LastName); }
}
[DataMember]
public string Identity { get; set; }
[DataMember(IsRequired = true)]
[Required(ErrorMessage = "Email is required")]
public string Email { get; set; }
[DataMember]
[Display(Name = "Birth Date")]
public DateTime? BirthDate { get; set; }
[DataMember]
public string Mobile { get; set; }
[DataMember]
public string Phone { get; set; }
[DataMember]
public string Twitter { get; set; }
[DataMember]
[Display(Name = "Facebook")]
public string FaceBook { get; set; }
[DataMember]
public string WebSite { get; set; }
[DataMember]
public string Blog { get; set; }
}
My CustomerModel object contains a property "CustomerBusinessId" is it possible for my viewmodel to exclude this property so I am only returning the required fields to the view?

Pass model to controller and send result to page not using form submit

I have a page I am writing to send emails to customers. One of the ways to select customers to email is to select products they have. Each product has different detail information and you will also be able to choose specifics about the product to narrow down who you are emailing.
Because this is going to be complex I need to do the processing to come up with the customer email list on the controller side but it would be a nightmare to try to pull all the data form the controls to send to it manually.
I would like to use an AJAX call that on the controller side would get the model tied to the view, query the database, and send back the list of emails so on the view I can pop up outlook for them with the list of email addresses already populated. Because I need to go back to the view with data I don't think I can do this with a form post which is how I normally get model into into the controller.
Does someone know how I could accomplish this?
Here are some of the clases I have to try to help people understand my layout
public class ProductType
{
[HiddenInput(DisplayValue = false)]
public int ID { get; set; }
[Required(ErrorMessage = "Please enter a product type description")]
public string Description { get; set; }
public virtual ICollection<ProductTypeDetail> ProductDetails { get; set; }
}
public class ProductTypeDetail
{
[HiddenInput(DisplayValue = false)]
public int ID { get; set; }
[HiddenInput(DisplayValue = false)]
public int? ProductTypeID { get; set; }
public ProductType ProductType { get; set; }
[Required(ErrorMessage = "Please enter a description")]
public string Description { get; set; }
[Required(ErrorMessage = "Please enter a valid type")]
public string Type { get; set; }
public virtual ICollection<ProductTypeDetailValidValue> ValidValues { get; set; }
}
The above 2 classes are for product types which someone could enter anything for and as many as they want. The product details are detail information you might need to know about your products. For example you could have a product type of Vehicle Registration System and put a product detail item in that for a specific import process that pertains to the product they you need to know if they use or not.
public Customer()
{
SiteVisits = new List<SiteVisit>();
Payments = new List<Payment>();
Contacts = new List<CustomerEmail>();
}
[HiddenInput(DisplayValue = false)]
public int ID { get; set; }
[Display(Name = "Name")]
[Required(ErrorMessage = "Please enter a customer name")]
public string CustomerName { get; set; }
[Display(Name = "Line 1")]
public string Address1 { get; set; }
[Display(Name = "Line 2")]
public string Address2 { get; set; }
[Display(Name = "Line 3")]
public string Address3 { get; set; }
[Display(Name = "Line 4")]
public string Address4 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
[HiddenInput(DisplayValue = false)]
[Required(ErrorMessage = "Please enter a customer type")]
public int CustomerTypeID { get; set; }
[Display(Name = "Type")]
public virtual CustomerType CustomerType { get; set; }
[HiddenInput(DisplayValue = false)]
[Required(ErrorMessage = "Please enter a customer status")]
public int CustomerStatusID { get; set; }
[Display(Name = "Status")]
public virtual CustomerStatus CustomerStatus { get; set; }
[DataType(DataType.MultilineText)]
public string Comments { get; set; }
[Required(ErrorMessage = "Please enter an expiration year")]
public long ExpirationYear { get; set; }
[Required(ErrorMessage = "Please enter an expiration month")]
public long ExpirationMonth { get; set; }
[Required(ErrorMessage = "Please enter a control name")]
public string ControlName { get; set; }
public Boolean Networked { get; set; }
public long Population { get; set; }
[Display(Name = "First Fiscal Month")]
public long FirstMonth { get; set; }
[Display(Name = "FTP User Name")]
public string FTPUserName { get; set; }
[Display(Name = "FTP Password")]
public string FTPPassword { get; set; }
[Display(Name = "Customer ID")]
public string CustomerUpdateID { get; set; }
[DataType(DataType.Date)]
[Display(Name = "Customer Since")]
public DateTime? StartDate { get; set; }
public virtual ICollection<CustomerPhoneNumber> PhoneNumbers { get; set; }
public virtual ICollection<CustomerProduct> Products { get; set; }
public virtual ICollection<CustomerEmail> Contacts { get; set; }
public virtual ICollection<SiteVisit> SiteVisits { get; set; }
public virtual ICollection<Payment> Payments { get; set; }
}
public class CustomerProduct
{
[HiddenInput(DisplayValue = false)]
public int ID { get; set; }
[HiddenInput(DisplayValue = false)]
public int? ProductTypeID { get; set; }
public virtual ProductType ProductType { get; set; }
[HiddenInput(DisplayValue = false)]
public int CustomerID { get; set; }
public virtual Customer Customer { get; set; }
[HiddenInput(DisplayValue = false)]
public int? VersionID { get; set; }
public virtual ProductVersion Version { get; set; }
[HiddenInput(DisplayValue = false)]
public int? StatusID { get; set; }
public virtual ProductStatus Status { get; set; }
public virtual ICollection<CustomerProductDetail> ProductDetails { get; set; }
}
public class CustomerProductDetail
{
[HiddenInput(DisplayValue = false)]
public int ID { get; set; }
[HiddenInput(DisplayValue = false)]
public int CustomerProductID { get; set; }
public virtual CustomerProduct CustomerProduct { get; set; }
[HiddenInput(DisplayValue = false)]
public int ProductTypeDetailID { get; set; }
public virtual ProductTypeDetail ProductTypeDetail { get; set; }
//[Required(ErrorMessage = "Please enter a value")]
public string Value { get; set; }
}
So above I have the customer class. Each customer can be set up with any number of the product types you have set up and you can select values for the product details of that product type for this particular customer. The customers also contain contacts. that is the class that has the email addresses.
So I need to show a screen that displays all the product types you have set up and lets you select values for detail items on products you select then I need to query and find customers that match this
All I needed to do was serialize the form and pass it as data in my ajax call. If on the Controller side that is getting called I have an argument that is of the same type as the model my view is strongly typed to the model binder is smart enough to fill in my object automatically

Resources