Malcom Graves

Malcom Graves

  • NA
  • 19
  • 609

Need to compare Value.From other table

Nov 4 2017 6:06 PM
i have 3 classes in my model
 
Model
  1. public class Student  
  2. {  
  3. public int id { getset; }  
  4. [Required]  
  5. [DisplayName("Student Name")]  
  6. public string Name { getset; }  
  7. [Required]  
  8. public int RollNO { getset; }  
  9. public DateTime EnrollmentDate { getset; }  
  10. public ICollection<Record> Records { getset; }  
  11. }  
  12.    
  13. public class Record  
  14. {  
  15. public int Id { getset; }  
  16. public int StudentId { getset; }  
  17. public int SubjectId { getset; }  
  18. [Required]  
  19. public int Marks { getset; }  
  20. public string Result { getset; }  
  21. public virtual Student Students { getset; }  
  22. public virtual Subject Subjects { getset; }  
  23. }  
  24.    
  25. public class Subject  
  26. {  
  27. public int id { getset; }  
  28. [Required]  
  29. public string Title { getset; }  
  30. [Required]  
  31. [DisplayName("Minimum Marks")]  
  32. public int MinMarks { getset; }  
  33. [Required]  
  34. [DisplayName("Max Marks")]  
  35. public int MaxMarks { getset; }  
  36. public ICollection<Record> Records { getset; }  
  37. }  
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 and error in any form possible...
 
This is my controller
 
  1.    
  2. public ActionResult Create([Bind(Include = "Id,StudentId,SubjectId,Marks,Result")] Record record,Subject subject)  
  3. {  
  4. if(record.Marks<record.Subjects.MinMarks)  
  5. {  
  6. record.Result = "Fail";  
  7. }  
  8. else  
  9. {  
  10. record.Result = "Pass";  
  11. }  
  12. if (ModelState.IsValid)  
  13. {  
  14. db.Records.Add(record);  
  15. db.SaveChanges();  
  16. return RedirectToAction("Index");  
  17. }  
  18. ViewBag.StudentId = new SelectList(db.Students, "id""Name", record.StudentId);  
  19. ViewBag.SubjectId = new SelectList(db.Subjects, "id""Title", record.SubjectId);  
  20. return View(record);  
  21. }  
 
 

Answers (3)