Model classes ::
public class MappedModels
{
[Key]
public int MappedId { get; set; }
public int MappedType { get; set; }
public string MappedDate { get; set; }
public decimal MappedAmount { get; set; }
[ForeignKey("ProjectId")]
public ProjectModels Project { get; set; }
public int ProjectId { get; set; }
[ForeignKey("SchoolId")]
public SchoolModels School { get; set; }
public int SchoolId { get; set; }
}
public class ProjectModels
{
[Key]
public int ProjectId { get; set; }
public string ProjectName { get; set; }
public decimal Amount { get; set; }
}
ViewModel class::
public class MappedViewModels
{
public int MappedType { get; set; }
public string MappedDate { get; set; }
public decimal MappedAmount { get; set; }
public int ProjectId { get; set; }
public string ProjectName { get; set; }
public decimal ProjectAllocatedAmount { get; set; } // assigned amount for each project
public decimal RemainingAmount { get; set; } // remaining amount from the project amount that can be allocated
public int SchoolId { get; set; }
public string SchoolName { get; set; }
}
I have omitted the school model class since it include only Name and Id.
MappedModels is used to assign project amount to different schools. Users have to select School name and Project Name from dropdowns. Then allocate project amount to that respective school.
Before allocating amount, I want to show users the actual Project Amount and the remaining amount for that Project.
Lets say example data in ProjectModels {1, MEEG203, 500000}
Supose this ProjectId is already mapped for a school with amount 300000.
Now if a user selects same ProjectId for allocating amount to another school, I want to show that: ProjectAllocatedAmount = 500000 RemainingAmount = 500000 - 300000 = 200000
I have already calculated the RemainingAmount assigned in variable all.
But this query is returning null value, why is it so? I am unable to understand what is wrong with this query.
public ActionResult Create(int? ProjectId)
{
// other code blocks ommitted...
var result = db.Mappeds.Include(p => p.Project)
.Where(p => p.ProjectId == ProjectId).Select(x => new MappedViewModels()
{
ProjectName = x.Project.ProjectName,
ProjectAllocatedAmount = x.Project.Amount,
SchoolName = "",
MappedAmount = 0,
RemainingAmount = x.Project.Amount - all
}).ToList().LastOrDefault();
return View(result);
}