ASP.NET MVC2: “System.MissingMethodException: No parameterless constructor defined for this object.”

﹥>﹥吖頭↗ 提交于 2019-12-02 07:19:02

I know you have this working now - which is great.

Just wanted to post a note here: Be careful when using SelectList in your Models. Your model will expect a SelectList but your action is probably returning id of the object selected - this will throw the

System.MissingMethodException: No parameterless constructor defined for this object.

You can handle it with something along these lines:

[Bind(Exclude = "List")]
public class UserFormModel
{
    public SelectList List { get; set; }
    public int SelectedItem { get; set; }
}

Just easy to miss and can be frustrating chasing down a parameter constructor error - so I wanted to note that here.

First, it's a bad idea to do data access in your model. Models shold be dumb, and they should not populate themselves. It's ok to create empty lists and such in the constructor, but don't do data access.

Second, Where you are doing data access, you're not disposing of the context. You should have a using statement there to automatically call Dispose on the context.

Third, don't use that "PropertiesMustMatch" attribute, instead use the Compare attribute on the ConfirmPassword property.

None of those things should cause the error you're seeing, though i'm not sure about the Attribute. Try removing it and seeing if it works.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!