I’m getting this error intermittently on my MVC 2 web application. My models do not have parameterless constructors, they look like this:
public AddParentModel(ITracktionDataLayer dataLayer, MessagePasser messager, AuthUserHelper authUser)
{
_model = new PersonAddEditModel(messager, dataLayer, authUser, null);
}
I should note that my controllers do have parameterless constructors. The PersonAddEditModel isn’t exposed directly to MVC. The AddParentModel exists only to make MVC binding easier.
Since the AddParentModel (and all other models that MVC is using) exist on the server, I could make them parameterless, although that would mess up my test methods a bit. The weird part about this is that it happens intermittently. I would expect an issue like this to happen consistently but that’s definitely not the case – I just get ELMAH sending me an email every other day or so with some unfortunate user getting this error.
I would like to know what my options are. The easiest way I can see of tackling this problem is to create a constructor overload that just sets these parameters to default objects (which I will probably do just to fix the issue at hand for now, but I feel so dirty doing it) but I would like to get input from people on the correct way to handle this. I’m relatively new to MVC. Thanks in advance!
EDIT: I wanted to add that in my [HttpPost] methods where the processing is done, even though I am using a model for MVC’s editing on the view, I am receiving back a FormCollection and use that to update the model, rather than have MVC edit the model directly.
/// <summary>
/// Add this person, then go to the next page where more people can be added.
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
[HttpPost]
public ActionResult Index(FormCollection fc, HttpPostedFileWrapper upload) {
Uri requestUri = !Request.Url.ToString().Contains("/127.0.0.1/") ? Request.Url : new Uri("...");
MessagePasser messager = new MessagePasser();
ITracktionDataLayer dataLayer = DataFactory.GetDataLayer();
AuthUserHelper user = AuthUserHelper.AnonymousWebUserFrom(dataLayer, requestUri);
AddParentModel model = new AddParentModel(dataLayer, messager, user);
if (TryUpdateModel(model, fc) && ModelState.IsValid)
{
...
I’m also realizing that, for some models, I cannot create a parameterless constructor because a few models require a couple extra primitives above and beyond my data layer, messaging layer and authenticated user.
I can’t use dependency injection to solve this because I am requesting extra parameters that are dependent on user information in the constructor. My constructor takes in IDataLayer, MessagePasser and AuthUserHelper, which is common for all the models, but I’m also asking for two int IDs, which rely on some hidden input fields or querystring parameters.
Therefore, I am going to split the properties of the model into it’s own class (the model) and the actual processing such as loading and saving into a service class. The model is just a property bag and can have a parameterless constructor, while the service class would be called to populate and save data. I hope this answer helps someone out there.
So, before I had:
Now I have split it into:
The model itself does not require any ctor parameters and should not throw that error anymore. I’m a big fan of encapsulating related logic and data into a single class but since this is just a wrapper around the heavy-lifting PersonAddEditModel and it’s only used on the MVC side, I can deal with this.
If anyone has any input or alternatives to what I just posted, I’m all ears.