I have a View that has some fields and I've added Model State validation in the Controller that checks if 2 specific fields are empty based on a value selected from a drop down list.
Here's my code that does the validation:
public ActionResult Create(Fund fund)
{
if (fund.FundTypeId == 4)
{
if (string.IsNullOrEmpty(fund.AccountType))
{
ModelState.AddModelError("AccountType", "Account Type required");
}
if (string.IsNullOrEmpty(fund.FilePath))
{
ModelState.AddModelError("FilePath", "File Path required");
}
}
}
Here my code checks the Model State to see if it's valid. It correctly shows that the Model State is false and doesn't perform the insert and redirect. However I'm unable to return to the current View with the fund model as a parameter to properly display the error next to the field that caused the error.
if (ModelState.IsValid)
{
// Perform the insert and redirect to Index page
}
// Need to have this view reflect the errors...
return View(fund);
I get this error:
The model item passed into the dictionary is of type 'Fund', but this dictionary requires a model item of type 'System.Web.Mvc.HandleErrorInfo'.
Update:
Based on the "duplicate" of my question I'd have to be passing the wrong model from my Controller to my View and that my View must have a model of "System.Web.Mvc.HandleErrorInfo" defined which certainly is not the case.
I'm not passing a model from a View to a Partial View nor am I declaring a model in the Layout file so I can rule that out.
I can also confirm that my View and Controller are using the same model types.
@using FundOfFunds_MVC.Models
@model Bank.Sec.Framework.Fund
@{
ViewBag.Title = "Create New FoF Fund";
var fundTypeModel = ViewData["FundType"] as FundTypeModel;
}
Update:
What's happening is after the Model State gets errors added to it, it triggers an OnException method in the Fund Controller which then calls an OnException methods in a Base Controller that it inherits from:
public class BaseController : Controller
{
private static readonly ILog databaseLogger = LogManager.GetLogger("DatabaseLogger");
protected override void OnException(ExceptionContext filterContext)
{
GlobalContext.Properties["PageName"] = filterContext.RouteData.Values["controller"];
GlobalContext.Properties["FunctionName"] = filterContext.RouteData.Values["action"];
databaseLogger.Error("MVC Controller Error", filterContext.Exception);
base.OnException(filterContext);
filterContext.ExceptionHandled = true;
this.View("Error").ExecuteResult(this.ControllerContext);
}
}
That last line where the "Error" view is being used is the culprit. This was code designed by folks that weren't very fluent in MVC so I'm trying my best to rectify this without reinventing the wheel.