如何提交包含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?
}
}
我收到以下错误:
没有为此对象定义无参数构造函数。
这样做的正确方法是什么?我需要使用编辑模板吗?
答案 0 :(得分:0)
我认为错误告诉你究竟出了什么问题。绑定到您的视图的东西缺少无参数构造函数。也许是PagedList?
此外,填充视图的操作使用的是与HttpPost方法不同的模型。 PagedClientViewModel与PagedList。在构建视图和提交视图时尝试使用相同的视图模型。