Error Msg : 'The ViewData item that has the key 'MethodCode' is of type 'System.String' but must be of type 'IEnumerable<SelectListItem>'.'
public class AssetBasisRuleViewModel
{
[DisplayName("Number of periods")]
public int? NumberOfPeriods { get; set; }
}
public sealed class AssetBasisRuleMethod : SmartCodes
{
public AssetBasisRuleMethod() { }
private AssetBasisRuleMethod(String meaning, String smartCode) : base(meaning, smartCode, "AssetBasisRuleMethod") { }
public static AssetBasisRuleMethod P { get { return new AssetBasisRuleMethod("Point-in-time", "P"); } }
public static AssetBasisRuleMethod A { get { return new AssetBasisRuleMethod("Average", "A"); } }
public static AssetBasisRuleMethod D { get { return new AssetBasisRuleMethod("ADB", "D"); } }
public static AssetBasisRuleMethod AA { get { return new AssetBasisRuleMethod("Average-of-averages", "AA"); } }
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(AssetBasisRuleViewModel model)
{
using (var dbContextScope = _dbContextScopeFactory.Create())
{
User user = _Login.GetCurrentUser();
if (!ModelState.IsValid)
{
model.PrepareSelectLists();
return View(model);
}
// Conditional validations
if (model.MethodCode == AssetBasisRuleMethod.A.SmartCode)
{
if (model.NumberOfPeriods == null)
{
ModelState.AddModelError("NumberOfPeriods", "The number of periods are required for averaging methods.");
return View(model);
}
}
else if (model.MethodCode == AssetBasisRuleMethod.AA.SmartCode)
{
model.NumberOfPeriods = 3;
}
else if (model.MethodCode == AssetBasisRuleMethod.P.SmartCode)
{
model.AveragePeriodCode = "P"; // Set to Period for non-average (not settable in UI)
}
// Default to "all" in period for valuation query when the rule type is ADB
else if (model.MethodCode == AssetBasisRuleMethod.D.SmartCode)
{
model.DateInPeriodCode = AssetBasisRuleDateInPeriod.A.SmartCode;
}
_repo.AddAssetBasisRule(abr, user);
dbContextScope.SaveChanges();
}
return RedirectToAction("Index");
}