1

How do I submit a form that contains a PagedList?

Model:

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; }
}

View:

@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" />
    }

View:

    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?
    }

}

I get the following error:

No parameterless constructor defined for this object.

What is the correct way to do this? Do I need to use an Editor Template?

woggles
  • 7,444
  • 12
  • 70
  • 130

1 Answers1

0

I think the error is telling you exactly what's wrong. Something that is bound to your view is missing a parameterless constructor. PagedList perhaps?

Also your action that populates your view is using a different model then your HttpPost method. PagedClientViewModel vs PagedList. Try using the same view model when your build your view and when you submit it.

Ben Tidman
  • 2,129
  • 17
  • 30