I have a project. And I need a custom validation for find minimum price. Now first off all my model is in here;
public class ReservationModel
{
[Required]
public DateTime BeginDate { get; set; }
[Required]
public DateTime EndDate { get; set; }
[Required]
public double Price { get; set; }
[Required]
public string EmailAddress { get; set; }
public Guid ApartId { get; set; }
[Required]
public int Guest { get; set; }
public string ApartName { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string ApartDesc { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
public string CategoryName { get; set; }
public double MinAmount { get; set; }
public string ApartImage { get; set; }
public string CheckInTime { get; set; }
public string CheckOutTime { get; set; }
public int DayOfReservation { get; set; }
public int ReservationGuestNumber { get; set; }
}
I want to use this validation in MinAmount. I have dynamic prices. For example apart price 300 USD. I want to MinAmount Value is must be 300 X 25 /100 = 75 USD. Validation error fire is If client write 50 USD or under 75 USD. So How can create My custom MinPriceAttribute??? Thanks for kindly replies.
What you can do is create your custom filter.
You can find some tips here mvc custom filter
Video tutorial
Related
Hi doing a master details and currently in the process of display two tables data based on invoice number but I have this error
An exception of type 'System.Data.Entity.Core.EntityCommandExecutionException' occurred in EntityFramework.SqlServer.dll but was not handled in user code
{"Invalid column name 'klsm_Invoice_InternalInvoiceNo'.\r\nInvalid column name 'klsm_Invoice_InternalInvoiceNo'.\r\nInvalid column name 'klsm_Invoice_InternalInvoiceNo'."}
I am following Tutorial: Implement CRUD Functionality with the Entity Framework in ASP.NET MVC but I have come to a problem.
Desc class
[Table("klsm_InvoiceDesc")]
public class Desc
{
[Key]
public string InternalInvoiceNo { get; set; }
public string InvoiceNo { get; set; }
public int DescNo { get; set; }
public string Principal { get; set; }
public string ChargeCode { get; set; }
public string Quantity { get; set; }
public string Description { get; set; }
public string UnitPrice { get; set; }
public string Amount { get; set; }
public string ForeignAmount { get; set; }
public string EL1 { get; set; }
public string EL2 { get; set; }
public string ShortName { get; set; }
public string InvoiceType { get; set; }
public string PONumber { get; set; }
public string Batch { get; set; }
public string CCVBatch { get; set; }
public string PaidAmount { get; set; }
public string Paid { get; set; }
public string TT { get; set; }
public string BankCode { get; set; }
//public string Id { get; set; }
public virtual College klsm_Invoice{ get; set; }
}
College class
[Table("klsm_Invoice")]
public class College
{
[Key]
public string InternalInvoiceNo { get; set; }
public string InvoiceNo { get; set; }
public DateTime InvoiceDate { get; set; }
public string CustomerName { get; set; }
public int Year { get; set; }
public int Month { get; set; }
public decimal Amount { get; set; }
public decimal ForeignAmount { get; set; }
public string UserCreated { get; set; }
public string UserModified { get; set; }
public int AccMonth { get; set; }
public int AccYear { get; set; }
public string AccStatus { get; set; }
public string Status { get; set; }
public string PaidStatus { get; set; }
public string Principal { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateModified { get; set; }
public string EL1 { get; set; }
public string InvoiceType { get; set; }
public string CurrencyType { get; set; }
public decimal ExchangeRate { get; set; }
public string GSTChecked { get; set; }
public DateTime PaymentSchedule { get; set; }
public string PaymentMode { get; set; }
public string CashAdvance { get; set; }
public virtual ICollection<Desc> klsm_InvoiceDesc { get; set; }
}
Home Controller
public ActionResult Details(string id)
{
College klsm_Invoice = db.klsm_Invoice.Find(id);
return View(klsm_Invoice);
}
I'm having the following situation: the Total from the Valid Nest Response is equals 2 and the Documents count equal 0. I'm not sure how that is possible.
Found the solution. The problem was in my model. You see, my model was something like that:
public class Customer
{
public string Id { get; set; }
public string Name { get; set; }
public string Nickname { get; set; }
public string PersonType { get; set; }
public string Gender { get; set; }
public string MaritalStatus { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{dd/MM/yyyy HH:mm:ss}")]
public DateTime? BirthDate { get; set; }
}
And this Index had Documents missing some fields, like:
public class Customer
{
public string Id { get; set; }
public string Name { get; set; }
public string PersonType { get; set; }
public string Gender { get; set; }
}
The solution was adding the decorator PropertyName from Nest, to Map my model, like this:
public class Customer
{
[PropertyName("id")]
public string Id { get; set; }
[PropertyName("name")]
public string Name { get; set; }
[PropertyName("nickname")]
public string Nickname { get; set; }
[PropertyName("personType")]
public string PersonType { get; set; }
[PropertyName("gender")]
public string Gender { get; set; }
[PropertyName("maritalStatus")]
public string MaritalStatus { get; set; }
[PropertyName("birthDate")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{dd/MM/yyyy HH:mm:ss}")]
public DateTime? BirthDate { get; set; }
}
Now it works!
For example, I have the following infrastructure class:
[Table("GeoHistory")]
public partial class GeoHistory
{
public int Id { get; set; }
public int CompanyId { get; set; }
public int DriverId { get; set; }
[StringLength(50)]
public string Name { get; set; }
public string Description { get; set; }
[StringLength(100)]
public string GeofenceLocation { get; set; }
[StringLength(100)]
public string GeofencePrevious { get; set; }
[StringLength(20)]
public string StateLocation { get; set; }
[StringLength(20)]
public string StatePrevious { get; set; }
public DateTime? DateTime { get; set; }
[StringLength(5)]
public string Heading { get; set; }
public decimal? Speed { get; set; }
[StringLength(50)]
public string Status { get; set; }
}
and the following View class (let's forget about domain layer):
public class GeoHistoryViewModel
{
public int Id { get; set; }
public int? CompanyId { get; set; }
public int? DriverId { get; set; }
[StringLength(50)]
public string Name { get; set; }
public string Description { get; set; }
}
as we can see, we edit only part of field list.
Now, we want to update data in DB. Of course, we can write like:
Infrastructure.Main.GeoHistory geoHistory = db.GeoHistories.Find(id);
geoHistory.CompanyId = model.CompanyId;
geoHistory.DriverId = model.DriverId;
geoHistory.Name = model.Name;
........
db.SaveChanges();
It works. But I want to use Automapper. And if I try to do the following:
Infrastructure.Main.GeoHistory geoHistory = mapper.Map<Infrastructure.Main.GeoHistory>(model);
db.GeoHistories.Attach(geoHistory);
db.Entry(geoHistory).State = EntityState.Modified;
db.SaveChanges();
It works, but of course remove values of fields, which are not exist in View Model, but exist in infrastructure class. How to use automapper, but don't lost these fields?
I have the following models. I'm trying to use Code First New Database approach with ASP.NET MVC 5.
Client.cs
public class Client
{
public int ID { get; set; }
public int Name { set; get; }
public string Phone { set; get; }
public string Email { get; set; }
public string Address { set; get; }
public Ibo IboID { set; get; }
public DateTime LastOrder { get; set; }
public virtual ICollection<Ibo> Ibo { set; get; }
}
Ibo.cs
public class Ibo
{
[Key]
public int IboID { set; get; }
[Required]
[Display(Name="Full Name")]
public string Name { set; get; }
[Required]
[RegularExpression(#"^\d+$", ErrorMessage = "The IBO number you've input must contain only numbers.")]
[Display(Name = "IBO Number")]
public string IboNumber { set; get; }
[Required]
[StringLength(100, ErrorMessage = "The Phone number must be at least {2} numbers long.", MinimumLength = 7)]
public string Phone { set; get; }
[Required]
public string Address { set; get; }
[Required]
[NotMapped]
public string Username { set; get; }
[Required]
[UIHint("EmailAddressAttribute")]
[NotMapped]
public string Email { set; get; }
[Required]
[DataType(DataType.Password)]
public string Password { set; get; }
[Display(Name="Confirm Password")]
[DataType(DataType.Password)]
[Compare("Password")]
[NotMapped]
public string PasswordConfirm { set; get; }
[NotMapped]
public string SelectedSecurityQ { set; get; }
[Display(Name="Security Question")]
public int SecurityQuestionID { set; get; }
[Required]
[Display(Name="Your answer")]
public string SecurityAnswer { set; get; }
public virtual SecurityQuestions SecurityQuestions { set; get; }
}
Mods.cs
public class Mods
{
public int ModsID { set; get; }
public int IboID { set; get; }
public virtual ICollection<Ibo> Ibo { set; get; }
}
Order.cs
public class Order
{
public int OrderID { set; get; }
public int IboID { set; get; }
public int ClientID { set; get; }
public DateTime Created { set; get; }
public DateTime Modified { set; get; }
public virtual ICollection<Ibo> Ibo { set; get; }
public virtual ICollection<Client> Client { set; get; }
}
OrderProcess.cs
public class OrderProcess
{
public int OrderProcessID {set; get;}
public int OrderID { set; get; }
public DateTime TimeStamp { set; get; }
public int ProductID { set; get; }
public int State { set; get; }
public decimal ProductCost { set; get; }
public virtual ICollection<Order> Order { set; get; }
public virtual ICollection<Product> Product { set; get; }
}
Product.cs
public class Product
{
public int ID;
public string ProductID;
public string ProductName;
public string ProductDesc;
public decimal Pv;
public decimal Bv;
public decimal PriceIbo_2014_09;
public decimal PriceSug_2014_09;
//If Product is taxable
public bool Tax;
public int State;
public decimal Price_Ibo_2014_09;
public decimal Price_Sug_2014_09;
public int Status;
}
SecurityQuestions.cs
public class SecurityQuestions
{
public int ID { set; get; }
public string SecurityQuestion { set; get; }
public virtual ICollection<Ibo> Ibo { get; set; }
}
What I'm trying to do is to set the following relations. I'm very confused on when and how to use ICollection<>, List<> or similars with the virtual keyboard.
I'm also trying to generate a new IboID entry in the Ibo table when a new user is created in the AspNetUsers table.
Something like this:
public virtual ICollection<Ibo> Iboes
Any references, suggestions will be kindly appreciated! Thanks in advance
I am completely new to ASP.NET and was trying to cross-reference two different models with each other. I have 'Customer':
public class Customer
{
public int CustomerID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string Zipcode { get; set; }
public string City { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
}
and 'Reservation':
public class Reservation
{
public int ReservationID { get; set; }
public string PetName { get; set; }
public DateTime Birthdate { get; set; }
public string Specie { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public Customer Customer { get; set; }
}
Now, each Reservation should belong to a Customer (as you can see in the bottom line of the Reservation model) and should therefore contain the CustomerID. On the other hand should each Customer contain references to each Reservation that was made by him.
How can I setup this relation?
Use an ICollection for a one to many relationship.
public class Customer
{
public int CustomerID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string Zipcode { get; set; }
public string City { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public ICollection<Reservation> Reservations { get; set; }
}
Note I originally suggested a generic list, but there's a few subtle differences.