MVC错误:没有为此对象定义的无参数构造函数

时间:2014-12-18 09:33:35

标签: asp.net-mvc

我有一个SearchController

  public ActionResult Index(int? page)
    {
        var vm = new SearchViewModel();
        var list = _db.Intrusts.ToList();
        int pageNumber = (page ?? 1);
        return View(new Tuple<SearchViewModel, PagedList.IPagedList<Intrust>>(vm, list.ToPagedList(pageNumber, 10)));
    }

    [HttpPost]
    public ActionResult Index(Tuple<SearchViewModel, PagedList.IPagedList<Intrust>> model)
    {
        var vm = model.Item1;
        var list = _db.Intrusts.ToList();
        if (vm.Code != null)
            list = list.Where(m => m.Code == vm.Code).ToList();


        return View("Index", new Tuple<SearchViewModel, PagedList.IPagedList<Intrust>>(vm, list.ToPagedList(1, 10)));
    }

并在视野中

@model Tuple<_6badgiri.ViewModels.SearchViewModel, PagedList.IPagedList<_6badgiri.Models.Intrust>>
@using (Html.BeginForm("Index", "Search", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary()
<div class="row">
  @Html.LabelFor(m => m.Item1.StateId)
  @Html.DropDownListFor(m => m.Item1.StateId, new SelectList(area, "Id", "Name"))
</div>
 <input type="submit" value="جستجو " id="btnsubmit" class="wpb_button btn td-login-button green" />
}

但是当我点击提交按钮时,我收到错误No parameterless constructor defined for this object.

2 个答案:

答案 0 :(得分:0)

问题是Tuple<T1, T2>没有默认构造函数,因此默认的模型绑定器无法对其进行初始化。

您应该使用其他合适的类型或实现自定义模型绑定器绑定到Tuple类型

答案 1 :(得分:0)

更改代码如下

这里我将第3行中的代码更改为“搜索到索引”。因为您使用搜索操作方法而不是索引

@model Tuple<_6badgiri.ViewModels.SearchViewModel, 
PagedList.IPagedList<_6badgiri.Models.Intrust>>
@using (Html.BeginForm("Index", "Search", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary()
<div class="row">
  @Html.LabelFor(m => m.Item1.StateId)
  @Html.DropDownListFor(m => m.Item1.StateId, new SelectList(area, "Id", "Name"))
</div>
 <input type="submit" value="جستجو " id="btnsubmit" class="wpb_button btn td-login-button green" />
}