我試圖根據querystring的值填充一個viewmodel。我的繼承人控制器:在IF語句中填充Viewmodel?
public ViewResult Index(int? ShiftStatusID)
{
//Get logged in User
User user = _accountService.GetUser(_formsAuthService.GetLoggedInUserID());
if (ShiftStatusID == null) // Get all shifts
{
ViewModelShiftList viewModel = new ViewModelShiftList
{
Shifts = _shiftService.GetShifts(user.organisationID.Value).ToList()
};
}
else // Get shifts by status id
{
ViewModelShiftList viewModel = new ViewModelShiftList
{
Shifts = _shiftService.GetShiftsByStatus(user.organisationID.Value, ShiftStatusID).ToList()
};
}
return View(viewModel);
}
所以它不會讓我的視圖模型迴歸的觀點,稱「視圖模型並不在當前的背景下存在」。它不會讓我在if語句之外聲明視圖模型。這應該怎麼做?
排列:我需要if語句內歸還視圖模型 – Dez79
把你的視圖模型聲明不在if語句中。默認情況下,你的代碼應該返回一個聲明的viewmodel。 –