Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have model classes for budgeting system,

public class BudgetModels
{
	[Key]
	public int BudgetId { get; set; }		
	public string BudgetName { get; set; }
	public decimal BudgetTotalAmount { get; set; }
}


public class ProjectModels
{
	[Key]
	public int ProjectId { get; set; }		
	public string ProjectName { get; set; }
	public decimal AllocatedAmount { get; set; }
	
	[ForeignKey("BudgetId")]
	public BudgetModels Budget { get; set; } 
	public int BudgetId { get; `set; }
}


In my view page(Create.cshtml) of ProjectModelsController -> Create Method,

above the textbox for AllocatedAmount I have to display the

readonly textbox for the values "BudgetTotalAmount" and "Remaining Amount"


"Remaining Amount" is a calculation field (BudgetTotalAmount - AllocatedAmount from all the ProjectModels where same BudgetId is used)

How can I show these two values in my view page "BudgetTotalAmount" and "Remaining Amount"?

Test Data::

BudgetId	BudgetName  	BudgetTotalAmount 
111			Road-const		15,00,000
112			Road-repair		25,00,000


ProjectId	ProjectName		AllocatedAmount		BudgetId
25			R-25			5,00,000			111
26			ABBS-56			2,00,000			111
27			ABBS-57			3,00,000			111

Now if I have to insert data for the next Project with BudgetId 111

then Remaining Amount would be (15,00,000 - (5,00,000 + 2,00,000 + 3,00,000)) = 5,00,000


What I have tried:

I have no idea how to approach for this problem?
Posted
Updated 12-Aug-20 23:38pm

1 solution

Well, a problem I might face is I dont know what records exist when .. lets say you have this record
BudgetId	BudgetName  	BudgetTotalAmount 
111			Road-const		15,00,000
If you want to 'insert data for the next Project with BudgetId 111 you 'get'/retrieve all the project records
ProjectId	ProjectName		AllocatedAmount		BudgetId
25			R-25			5,00,000			111
26			ABBS-56			2,00,000			111
27			ABBS-57			3,00,000			111
and then sum the Allocated amount - then you have the two amounts you need, yes ? .. if you have a list of these records, you can use LINQ to select by BudgetId 111 and sum AllocatedAmount
 
Share this answer
 
Comments
nischalinn 13-Aug-20 6:14am    
Thank You Garth for your suggestion. But how to convert this to code?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900