我有我的控制器方法中下面的代碼來提供數據,列表視圖會員類型:如果內部列表視圖控制器的方法聲明
[HttpGet]
public ActionResult SelectMembershipType(int clubId)
{
//Populate the list
var membershipTypes = from s in db.MembershipTypes
where (s.ClubId == clubId && s.Dormant == false)
orderby s.Type
select s;
//Generate the ViewModel with the appropriate membership types
var viewModel = membershipTypes.Select(t => new SelectMembershipTypeViewModel
{
//Select appropriate Cost based on current month
if(DateTime.Now.Month == t.ReducedMonth)
{
Cost = t.ReducedCost;
}
else
{
Cost = t.Cost;
}
ClubId = club.ClubId,
Name = club.Name,
MembershipName = t.Type,
MembershipType = t.MembershipTypeClassification.Type,
MembershipTypeId = t.MembershipTypeId,
});
return View(viewModel);
}
if語句伊夫放在不起作用它拋出幾個錯誤。 我試圖將if語句應用於Cost的值,即如果今天的月份等於數據庫中每個成員資格的ReducedMonth類型使成本的值等於成員資格類型ReducedCost值,如果不是使其等於其成本代替。 每個MembershipType可以有不同的減小月份與成本
我不知道正確的語法的實現代碼正確
什麼是確切的錯誤信息? – ekad
您的問題已回答兩次:http://stackoverflow.com/questions/2234091/object-initializer-and-dynamically-specifying-properties和http://stackoverflow.com/questions/3229188/c-sharp-initialiser-有條件分配 –