1

如何提交包含 PagedList 的表单?

模型:

public class PagedClientViewModel
{
    public int? Page { get; set; }
    public PagedList.PagedList<ClientViewModel> Clients { get; set; }
}

public class ClientViewModel
{
    public int ClientId { get; set; }
    public string Name { get; set; }
    public bool IsCool { get; set; }
}

看法:

@model MvcApplication21.Models.PagedClientViewModel

@{
    ViewBag.Title = "Index";
}



 @using (Html.BeginForm("index", "home"))
    {         
        foreach (var item in Model.Clients)
        { 
    <div>
    @Html.HiddenFor(modelItem => item.ClientId)
    @Html.EditorFor(modelItem => item.Name)
    @Html.CheckBoxFor(modelItem => item.IsCool)
    </div>        
        }

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

看法:

    public ActionResult Index()
    {
        List<ClientViewModel> clients = new List<ClientViewModel>();

        ClientViewModel client1 = new ClientViewModel
        {
            ClientId = 1,
            Name = "Bob",
            IsCool = false
        };
        ClientViewModel client2 = new ClientViewModel
        {
            ClientId = 2,
            Name = "John",
            IsCool = false
        };
        ClientViewModel client3 = new ClientViewModel
        {
            ClientId = 3,
            Name = "Peter",
            IsCool = true
        };

        clients.Add(client1);
        clients.Add(client2);
        clients.Add(client3);

        PagedClientViewModel model = new PagedClientViewModel
        {
           Page = 1,
           Clients = new PagedList<ClientViewModel>(clients, 1, 10)           
        };

        return View(model);
    }

    [HttpPost]
    public void Index(PagedList<ClientViewModel> model)
    {
       //how do i process the model here?
    }

}

我收到以下错误:

没有为此对象定义无参数构造函数。

这样做的正确方法是什么?我需要使用编辑器模板吗?

4

1 回答 1

0

我认为该错误正在告诉您到底出了什么问题。绑定到您的视图的东西缺少无参数构造函数。PagedList 也许?

此外,您填充视图的操作使用的模型与您的 HttpPost 方法不同。PagedClientViewModel 与 PagedList。在构建视图和提交视图时尝试使用相同的视图模型。

于 2013-01-11T14:03:11.910 回答