Need to compare value from other table - c#

I have 3 classes in my model
Model
public class Student
{
public int id { get; set; }
[Required]
[DisplayName("Student Name")]
public string Name { get; set; }
[Required]
public int RollNO { get; set; }
public DateTime EnrollmentDate { get; set; }
public ICollection<Record> Records { get; set; }
}
public class Record
{
public int Id { get; set; }
public int StudentId { get; set; }
public int SubjectId { get; set; }
[Required]
public int Marks { get; set; }
public string Result { get; set; }
public virtual Student Students { get; set; }
public virtual Subject Subjects { get; set; }
}
public class Subject
{
public int id { get; set; }
[Required]
public string Title { get; set; }
[Required]
[DisplayName("Minimum Marks")]
public int MinMarks { get; set; }
[Required]
[DisplayName("Max Marks")]
public int MaxMarks { get; set; }
public ICollection<Record> Records { get; set; }
}
In subject table i will be creating each subject and setting its minimum and maximum marks required to pass...now in record table (Create Page) i want to compare the selected subject minimum marks with Record.Marks and if its less the minimum marks get Fail in Record.Result and if its greater then maximum marks get Pass in Record.Result...and i also want to compare the Result.Marks property with Subject.MaxMarks and if its greater then Subject.MaxMarks the user should get error in any form possible...
this is my controller
Controller
public ActionResult Create([Bind(Include = "Id,StudentId,SubjectId,Marks,Result")] Record record,Subject subject)
{
var selectedSubject = db.Subjects.Where(sub => subject.id == record.SubjectId).FirstOrDefault();
if (record.Marks < selectedSubject.MinMarks)
{
record.Result = "Fail";
}
else
record.Result = "Pass";
if (ModelState.IsValid)
{
db.Records.Add(record);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.StudentId = new SelectList(db.Students, "id", "Name", record.StudentId);
ViewBag.SubjectId = new SelectList(db.Subjects, "id", "Title", record.SubjectId);
return View(record);
}

Assuming your view has the input element to select/enter the subject id and the element's name attribute value is SubjectId and it is inside the form
Just add an else if to check max mark as well. You do not need the second parameter in your action method. Just remove it.
var selectedSubject = db.Subjects.FirstOrDefault(a=> a.id == record.SubjectId);
if(selectedSubject!=null)
{
if (record.Marks < selectedSubject.MinMarks)
{
record.Result = "Fail";
}
else if (record.Marks > selectedSubject.MaxMarks)
{
record.Result = "Error";
}
else
{
record.Result = "Pass";
}
}
else
{
ModelState.AddModeError(string.empty,"Subject not found");
}
//to do : Reload dropdown data and return the view

Related

ModelState missing values

i am trying to create a Menu for diffent Roles but when i choose the menu and role to create a Role_has_menu something is missing and ModelState said valid = false.
This is the method i am using
public IActionResult Create()
{
ViewData["MenuID"] = new SelectList(_context.Menu, "ID", "FullName");
ViewData["RoleID"] = new SelectList(_context.Role, "ID", "Name");
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("ID,MenuID,RoleID")] RoleHasMenu roleHasMenu)
{
if (ModelState.IsValid)
{
_context.Add(roleHasMenu);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
ViewData["MenuID"] = new SelectList(_context.Menu, "ID", "FullName", roleHasMenu.MenuID);
ViewData["RoleID"] = new SelectList(_context.Role, "ID", "Name", roleHasMenu.RoleID);
return View(roleHasMenu);
}
Those are my models
public class Menu
{
public int ID { get; set; }
[Required(ErrorMessage = "The element {0} can't be empty")]
public string Controller { get; set; }
[Required(ErrorMessage = "The element {0} can't be empty")]
public string Action { get; set; }
[Required(ErrorMessage = "The element {0} can't be empty")]
public string Label { get; set; }
[InverseProperty("RoleHasMenu")]
List<RoleHasMenu> roleHasMenu { get; set; }
[NotMapped]
public string FullName { get => Controller + " - " + Action; }
}
public class Role
{
public int ID { get; set; }
[Required(ErrorMessage = "The element {0} can't be empty")]
[NotNull]
public string Name { get; set; }
[MaxLength(100, ErrorMessage = "This element can't be bigger than 100 characters")]
public string Description { get; set; }
[InverseProperty("UserAccount")]
List<UserAccount> userAccount { get; set; }
[InverseProperty("RoleHasMenu")]
List<RoleHasMenu> roleHasMenu { get; set; }
}
public class RoleHasMenu
{
[Key]
public int ID { get; set; }
[ForeignKey("MenuID")]
public Menu Menu { get; set; }
public int MenuID { get; set; }
[ForeignKey("RoleID")]
public Role Role { get; set; }
public int RoleID { get; set; }
}
and this is my problem
I have the same code in other project and it works but this time i am using Visual Studio EF6 and a MySQL database instead of SQLserver
Thanks for your time

ASP .NET Core MVC - How to initialize an object property in a model class which is a result of many-to-many relationship?

When I want to call a property of my object property Trip from TripApplicationUser model class its values are null. So I do not know how to initialize the Trip object to get its property values later on and to now have problem with indexing in database. I have pasted here the most important parts of code.
[Authorize]
public async Task<ActionResult> Enroll(int id)
{
if (id == null)
{
return NotFound();
}
var currentTrip = await _context.Trip.FindAsync(id);
var currentUser = await _userManager.GetUserAsync(User);
var isAlreadyEnrolled = _context.TripApplicationUsers.Where(tu => tu.ApplicationUserId.Equals(currentUser.Id) && tu.TripId == id);
var UserTrips = isAlreadyEnrolled.ToList();
if (currentTrip.TripSeats > 0 && !UserTrips.Any())
{
ViewBag.process = "done";
currentTrip.TripSeats--;
_context.Update(currentTrip);
var rowToSave = new TripApplicationUser
{
TripId = currentTrip.TripId,
ApplicationUserId = currentUser.Id,
Trip = currentTrip //HOW SHOULD I INITIALIZE IT ACTUALLY?
};
_context.Add(rowToSave);
await _context.SaveChangesAsync();
} else if (UserTrips.Any())
{
ViewBag.process = "already done";
} else if(currentTrip.TripSeats <= 0)
{
ViewBag.process = "not done";
}
var UsersTrips = _context.TripApplicationUsers.Where(t => t.ApplicationUserId.Equals(currentUser.Id)).ToList();
return View(UsersTrips);
}
public class ApplicationUser : IdentityUser
{
[PersonalData]
[Column(TypeName = "nvarchar(MAX)")]
public string FirstName { get; set; }
[PersonalData]
[Column(TypeName = "nvarchar(MAX)")]
public string Surname { get; set; }
[PersonalData]
[Column(TypeName = "nvarchar(MAX)")]
public string BirthDate { get; set; }
public ICollection<TripApplicationUser> TripApplicationUsers { get; set; }
}
public class Trip
{
public int TripId { get; set; }
public string TripDate { get; set; }
public int TripDuration { get; set; }
public int TripLength { get; set; }
public int TripSeats { get; set; }
public int TrailId { get; set; }
public Trail Trail { get; set; }
public ICollection<TripApplicationUser> TripApplicationUsers { get; set; }
}
public class TripApplicationUser
{
public int TripId { get; set; }
public Trip Trip { get; set; }
public string ApplicationUserId { get; set; }
public ApplicationUser ApplicationUser { get; set; }
}
If you want your Trip object to contain data from Navigational properties you have to include them in the request.
var currentTrip = await _context.Trip.Include(trip=> trip.TripApplicationUsers).FirstOrDefaultAsync(trip => trip.TripId == id);

Can't save changes in EF Core : an error occurred while updating the entities. Invalid Object Name 'CartClothesItem'

I use a many-to-many relationship. When I want to add an item to the cart I get this exception:
An error occurred while updating the entities. Invalid object name 'CartClothesItem'
I don't even have the entity named 'CartClothesItem'.
This is my controller code:
[HttpPost("Add")]
public async Task<ActionResult> AddItemIntoCart(int userId, int itemId)
{
if (userId <= 0 || itemId <= 0)
return BadRequest("The parameters are invalid!");
var userCart = _context.Carts.SingleOrDefault(c => c.UserId == userId);
var item = _context.ClothesItems.SingleOrDefault(i => i.ClothesItemId == itemId);
if (item == null)
return NotFound("The item doesn't exist");
if (userCart == null)
return NotFound("The user doesn't exist");
if (userCart.Items.Where(i => i.ClothesItemId == itemId).Any())
return BadRequest("You already have this item in the cart");
userCart.Items.Add(item);
await _context.SaveChangesAsync();
return Ok("The item was successfully added into your cart!");
}
Item model class:
public class СlothesItem
{
[Key]
public int ClothesItemId { get; set; }
[Required]
public string ModelName { get; set; }
[Required]
public string Brand { get; set; }
[Required]
public string Size { get; set; }
public string Description { get; set; }
[Range(0.0, Double.MaxValue, ErrorMessage = "The price can't be negative.")]
public double Price { get; set; }
[Required]
[ClothesItem_EnsureThisClothesTypeExists]
public string Type { get; set; }
public virtual ICollection<Photo> Photos { get; set; }
[JsonIgnore]
public virtual ICollection<Cart> Carts { get; set; }
public СlothesItem()
{
Photos = new List<Photo>();
Carts = new List<Cart>();
}
}
Cart model class:
public class Cart
{
[Key]
public int CartId { get; set; }
public int UserId { get; set; }
[ForeignKey(nameof(UserId))]
public User User { get; set; }
public virtual ICollection<СlothesItem> Items { get; set; }
public Cart()
{
Items = new List<СlothesItem>();
}
}

Building a simple book reservation system

Everything works fine and I am almost done, but would like to add a restriction regarding users age. To be specific, if a user tries to make a reservation for book that has a category of "Adults" (id=3), she/he should be at least 18 years old, otherwise validation error should occur.
Classes:
public class Book
{
[Key]
public int Id { get; set;}
public string Title { get; set; }
public string Author { get; set; }
public int Year { get; set; }
public int Remaining { get; set; }
public int Borrowed { get; set; }
[ForeignKey("Category")]
public int CategoryId { get; set; }
public Category Category { get; set; }
public string Available { get; set; }
}
public class Lending
{
[Key]
public int Id { get; set; }
[ForeignKey("Book")]
public int BookId { get; set; }
public virtual Book Book { get; set; }
public string UserId { get; set; }
[DataType(DataType.Date)]
public DateTime? StartDate { get; set; }
[DataType(DataType.Date)]
public DateTime? EndDate { get; set; }
}
public class Category
{
[Key]
public int Id { get; set; }
public string CategoryName { get; set; }
[DataType(DataType.Currency)]
public decimal Price { get; set; }
}
I have three categories in my database, Children(Id=1), Young(Id=2) and Adults(Id=3).
One of the ViewModels:
public class LendingViewModel
{
public int Id { get; set; }
[ForeignKey("Book")]
public int BookId { get; set; }
public virtual Book Book { get; set; }
public string Title { get; set; }
public string Author { get; set; }
public string UserId { get; set; }
[DataType(DataType.Date)]
public DateTime? StartDate { get; set; }
[DataType(DataType.Date)]
public DateTime? EndDate { get; set; }
public string CategoryName { get; set; }
public decimal Price { get; set; }
public int Year { get; set; }
public string AvailableNow { get; set; }
public int RemainingNow { get; set; }
}
Changes in IdentityModels:
public class ApplicationUser : IdentityUser
{
public DateTime? BirthDate { get; set; } = DateTime.Now;
}
LendingController (just a part that is relevant):
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(LendingViewModel vm)
{
string userId = User.Identity.GetUserId();
vm.UserId = userId;
var user = new ApplicationUser();
user.Id = userId;
var age = DateTime.Now.Year - user.BirthDate.Value.Year;
var booksa = new Book();
booksa.Id = vm.BookId;
if (ModelState.IsValid && vm != null)
{
if (age < 18 && booksa.CategoryId == 3)
{
ModelState.AddModelError("", "You are to young!");
return View(vm);
};
I also made necessary changes in RegisterViewModel and Register View. However, when a user that is younger than 18 selects a category with the id of 3 (Adults), she/he is still able to make a reservation for the book. What is wrong with my code?
Solved! I used static method and combined it with the method from my repository. Here it is:
Repository:
public bool BookForAdults(int bookId)
{
return db.Books.Where(b => b.Id == bookId).Any(b =>
b.CategoryId == 3);
}
Extension:
public static int GetUserAge(this IIdentity identity)
{
var db = ApplicationDbContext.Create();
var user = db.Users.FirstOrDefault(u =>
u.UserName.Equals(identity.Name));
return user.Age;
}
Controller:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(LendingViewModel vm)
{
string userId = User.Identity.GetUserId();
vm.UserId = userId;
var age = User.Identity.GetUserAge();
var bookForAdults =
repo.BookForAdults(vm.BookId);
if (ModelState.IsValid && vm != null)
{
if(age < 18 && bookForAdults)
{
ModelState.AddModelError("", "You are to
young!");
return View(vm);
}

ViewModel save selected value from dropdownlist

I have a dropdownlist - difficult. This is a dropdonwlinst where the user can select a value(easy, hard) but the value is not saved to the database, if I select a value. The strange thing I also have Country list, and that value is saved to the database.And If I put the values by hand in the database - for difficult the values are shown in the view
ModelVIew:
public RouteViewModel()
{
Countries = new List<SelectListItem>();
DifficultGrades = new List<SelectListItem>();
}
public int Id { get; set; }
[Required(ErrorMessage = "You need to give it a name")]
public string Name { get; set; }
public int SelectedValue { get; set; }
public int SelectedId { get; set; }
public IEnumerable<SelectListItem> Countries { get; set; }
public IEnumerable<SelectListItem> DifficultGrades { get; set; }
}
public class Difficult
{
[Key]
public int DifficultId { get; set; }
public string DifficultName { get; set; }
public virtual ICollection<Route> Routes { get; set; }
}
public class Route
{
[Key]
public int routeID { get; set; }
public string Name { get; set; }
public int? UserProfileID { get; set; }
public int? CountryID { get; set; }
public int? DifficultGradeID { get; set; }
public virtual UserProfile userProfile { get; set; }
public virtual Country country { get; set; }
public virtual Difficult difficult { get; set; }
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(RouteViewModel routeViewModel )
{
try
{
if (ModelState.IsValid)
{
var route = new Route();
UpdateRoute(route, routeViewModel);
db.Routes.Add(route);
db.SaveChangesAsync();
return RedirectToAction("Index");
}
}
catch (RetryLimitExceededException /* dex */)
{
ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
}
ViewBag.Id = new SelectList(db.countries, "Id", "country_name", routeViewModel.Id);
ViewBag.DifficultId = new SelectList(db.difficulties, "DifficultId", "DifficultName", routeViewModel.Id);
return View(new RouteViewModel());
}
and the update method:
public void UpdateRoute(Route route, RouteViewModel routeViewModel )
{
route.routeID = routeViewModel.Id;
route.Name = routeViewModel.Name;
route.CountryID = routeViewModel.Id;
route.DifficultGradeID = routeViewModel.Id;
// climb.country.country_name = ModelViewClimb.Name;
}
Thank you

Categories