System.MissingMethodException:没有为此对象定义无参数构造函数 [英] System.MissingMethodException: No parameterless constructor defined for this object

查看:76
本文介绍了System.MissingMethodException:没有为此对象定义无参数构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有 Html.ListBoxFor 的 MVC 2.0,如下所示:

I'm using MVC 2.0 with a Html.ListBoxFor as below:

<% using (Html.BeginForm()) { %>

       <input type="submit" value=">" />

        <%= Html.ListBoxFor(x => x.lstTest, new MultiSelectList(new [] {"someone", "crap", "why"})) %>

    <% } %>

当我单击下面的输入提交按钮而未选择任何内容时,它会很好地回发,当我选择列表框中的 3 个项目之一时,它会引发此错误:

When I click the input submit button below with nothing selected, it posts back fine, when I select one of the 3 items in the listbox it throws this error:

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

有什么想法吗?这是我的控制器代码:

Any ideas? here is my controller code:

 [HandleError]
public class HomeController : Controller
{
    public HomeController()
    {

    }

    public ActionResult Index()
    {
        ViewData["Message"] = "Test Harness";

        return View();
    }

    [HttpGet]
    public ActionResult About()
    {
        ViewData["mykey"] = "Test Harness";

        LogOnModel model = new LogOnModel();
        model.lstTest = new MultiSelectList(new [] {"A", "B", "C"});

        return View(model);
    }


    [HttpPost]
    public ActionResult About(LogOnModel model)
    {
        ViewData["mykey"] = "Test Harness";

        model.lstTest = new MultiSelectList(new [] { "" });

        return View(model);
    }
}

推荐答案

你的 LogOnModel 有无参数构造函数吗?它需要一个 DefaultModelBinder 来实例化它.此外,当您发布异常时,请发布异常对象的完整堆栈跟踪,否则我们只是在猜测错误实际发生的位置.

Does your LogOnModel have a parameterless constructor? It needs one for the DefaultModelBinder to instantiate it. Additionally, when you post the exception, please post the full stack trace from the exception object, else we're simply guessing where the error actually took place.

这篇关于System.MissingMethodException:没有为此对象定义无参数构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆