0

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);
}

1 Answers1

0

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
Shyju
  • 214,206
  • 104
  • 411
  • 497
  • its not working like that...im getting null at selectedsubject.minmarks – Muhammad Aahad Nov 05 '17 at 13:08
  • Are you sure you have the correct subjectId value in `record.SubjectId` ? – Shyju Nov 05 '17 at 13:10
  • See the updated answer. This will give you the subject `var selectedSubject = db.Subjects.FirstOrDefault(a=> a.id == record.SubjectId);` when your `record.SubjectId` has the correct value. If `record.SubjectId` is not a valid subject Id, that means you are not sending it from the view (form). Put a breakpoint in your action method and inspect this value first. – Shyju Nov 05 '17 at 13:15
  • Your LINQ query to get the selected subject is wrong. Use the one i have in question. Also you do not need the second parameter `,Subject subject` – Shyju Nov 05 '17 at 13:17
  • thanks...im getting value in selectedsubject...but now im not able to insert any data not getting redirected to index after clicking create... – Muhammad Aahad Nov 05 '17 at 13:27
  • are you getting an exception ? If yes check the inner exception/stack trace of the exception and you will know why it if failing – Shyju Nov 05 '17 at 13:32
  • no exception is just nothing is happening i try to create after removing the if statements still nothing gonna try rebuilding the application... – Muhammad Aahad Nov 05 '17 at 13:34
  • Is `ModelState.IsValid` returns true? Put a breakpoint in your code and use step over on each line (press F10) so you can see which lines are executing. That will give you an idea – Shyju Nov 05 '17 at 13:35
  • Did you remove `Subject` parameter from the action method ? – Shyju Nov 05 '17 at 13:36
  • yeah model state is not valid – Muhammad Aahad Nov 05 '17 at 13:40
  • So find out why it is not valid and fix that. You can read the model state error if you want. Take a look at [How to figure out which key of ModelState has error](https://stackoverflow.com/questions/15296069/how-to-figure-out-which-key-of-modelstate-has-error) – Shyju Nov 05 '17 at 13:42
  • nvm thanks for all your help is working again...Subject parameter was not remove thought i did it...Thanks for all your help... – Muhammad Aahad Nov 05 '17 at 13:43