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.
Related
Hi everyone I cant seem to figure out how to check a record before creating a new post in MVC
When a user creates a "SuperMember" I want it to check IF that "Character" has already been assigned that "membership" already aka a duplicate record. Each Character can have many memberships but not the same one.
Heres my code -
MODELS
public class Character
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CharacterID { get; set; }
[Required]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required]
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Required]
[Display(Name = "Hero Alias")]
public string HeroAlias { get; set; }
[Required]
[Display(Name = "Email Address")]
public string Email { get; set; }
public ICollection<SuperMembers> SuperMembers { get; set; }
}
public class Membership
{
[Key]
public int MembershipID { get; set; }
[Required]
[Display(Name = "Membership Name")]
public string MembershipName { get; set; }
[Required]
[Display(Name = "Membership Tyoe")]
public string Type { get; set; }
public ICollection<SuperMembers>SuperMembers { get; set; }
}
public class SuperMembers
{
[Key]
[Display(Name = "Membership Number")]
public int SuperMembersID { get; set; }
[Required]
[Display(Name = "Hero Alias")]
public int CharacterID { get; set; }
public Character Character { get; set; }
[Required]
[Display(Name = "Membership")]
public int MembershipID { get; set; }
public Membership Membership { get; set; }
[DataType(DataType.Currency)]
[Column(TypeName = "money")]
[Required]
[Display(Name = "Account Balance")]
public decimal AccountBalance { get; set; }
}
Then the Controller POST Method
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("SuperMembersID,CharacterID,MembershipID,AccountBalance")] SuperMembers superMembers)
{
if (ModelState.IsValid)
{
var IfAlreadyExists = dbo.SuperMembers.Where(x => x.CharacterID == CharacterID && x.MembershipID == MembershipName).FirstOrDefault();
if (IfAlreadyExists == null)
{
//POST
}
else
{
//Return ERROR "Sorry this Membership already exists"
}
_context.Add(superMembers);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
ViewData["CharacterID"] = new SelectList(_context.Character, "CharacterID", "HeroAlias", superMembers.CharacterID);
ViewData["MembershipID"] = new SelectList(_context.Membership, "MembershipID", "MembershipName", superMembers.MembershipID);
return View(superMembers);
}
The dbo in "dbo.SuperMembers" is underlined too, im using a local db for this little practice project so not sure if thats also a factor?
Im fairly new to this, this is my first StackOverflow post so apologies if ive missed anything obvious - Would appreciate any pointers. Thanks
HAVING TROUBLE GETTING MY EVENTS CONTROLLER TO SHOW IN BROWSER. I AM RECEIVING ERROR MESSAGE THAT MY COLUMN NAME IS INVALID
SqlException: Invalid column name 'City'.
Invalid column name 'State'.
EVENT MODEL code
public class Events
{
[Key]
public int EventsId { get; set; }
[Required(ErrorMessage = "Title is requied")]
[StringLength(50, ErrorMessage = "Title cannot be more than 50 characters")]
public string EventTitle { get; set; }
[StringLength(150, ErrorMessage = "Description should not exceed 150 characters")]
public string EventDescription { get; set; }
[Required]
public DateTime EventStartTime { get; set; }
[Required(ErrorMessage = "Start Date cannot be in the past")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0;MMdd/yyyy}")]
public DateTime EventStartDate { get; set; }
[Required]
public DateTime EventEndTime { get; set; }
[Required(ErrorMessage = "End date cannot be less than Start date")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0;MMdd/yyyy}")]
public DateTime EventEndDate { get; set; }
public string City { get; set; }
public string State { get; set; }
public string EventType { get; set; }
[Required]
public string OrganizerName { get; set; }
public string OrganizerContactInfo { get; set; }
[Required(ErrorMessage = " Max tickets cannot be 0")]
public int MaxTickets { get; set; }
[Required(ErrorMessage = "Avaliable tickets cannot be 0")]
public int AvaliableTickets { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
throw new NotImplementedException();
}
}
}
here
enter image description here
There is a lot of information missing. My guess would be the Events table in your database does not contain City or State columns.
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
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?
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