我有一個MVC應用程序。以下代碼正在控制器中的多個位置和多個控制器中使用。我想將這些代碼放在一個地方,並從每個位置調用它。 MVC中最好的方法是什麼?mvc常見代碼的最佳做法
以下代碼從數據庫獲取一行,並創建可從視圖中讀取的ViewData。使用webforms,我會在類中創建一個公共子類並傳遞年份和月份值。這種代碼是否可以成爲模型的一部分?
var monthlyexpenseincome = (from vu_monthlyresult in dbBudget.vu_MonthlyResults
where vu_monthlyresult.Month == defaultmonth && vu_monthlyresult.Year == defaultyear
select vu_monthlyresult).Single();
var yearlyexpenseincome = (from vu_yearlyresult in dbBudget.vu_YearlyResults
where vu_yearlyresult.Year == defaultyear
select vu_yearlyresult).Single();
ViewData["MonthlyExpenses"] = monthlyexpenseincome.Expenses;
ViewData["MonthlyIncome"] = monthlyexpenseincome.Income;
ViewData["MonthlyProfit"] = monthlyexpenseincome.Income - monthlyexpenseincome.Expenses;
查看Facade模式以處理您的數據查詢。 – Ammar 2012-08-10 20:20:17
檢查存儲庫模式 – JOBG 2012-08-10 22:22:22