MVC ViewModel Error - No parameterless constructor defined for this object

后端 未结 3 973
一个人的身影
一个人的身影 2020-12-09 12:52

I would like to know how can I use my ViewModel on the Create Action? I tried several examples I found here in the forum, but none solved my problem. I\'ve been racking my b

3条回答
  •  温柔的废话
    2020-12-09 13:18

    Similar to Erik Funkenbusch's answer I'd added a DropDownList to my form, however in my case it wasn't (and wasn't intended to be) submitted with the form as it was outside of the

    tags:

    @Html.DropDownList("myField", Model.MyField)
    

    As the Model contained the field for display only, this also caused the No parameterless constructor defined for this object error because the field wasn't submitted at all.

    In this case I fixed it by adding an exclude binding:

    public ActionResult Foo(int id, int? page, [Bind(Exclude = "MyField")]MyModel model)
    

提交回复
热议问题