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?

2
  • When do you get this error? When you submit your form?
    – Ben Tidman
    Jan 11, 2013 at 13:16
  • Yep when clicking the submit button
    – woggles
    Jan 11, 2013 at 13:49

1 Answer 1

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.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.