4

我有以下 PagedListModel:

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

public class ClientViewModel
{        
    public string ClientNumber { get; set; }
    public bool UseThisClient{ get; set; }
}

我的观点是这样的:

@using (Html.BeginForm("Index", "Home", FormMethod.Get, new { id = "Form" }))
{
    @foreach (var item in Model.Clients)
    {
       @Html.DisplayFor(modelItem => item.ClientNumber)
       @Html.CheckBoxFor(modelItem => item.UseThisClient)
    }    

 @Html.HiddenFor(model => model.Clients)            
}

控制器动作:

 public ActionResult Index(PagedClientViewModel model)
 {
  //...process all clients in the list
 }

我想将模型发布回控制器,以便我可以处理已勾选的复选框,但我收到以下错误:我有点理解该错误是因为我正在回发一个接口但我找不到方法周围。我怎样才能得到这份工作?

无法创建接口的实例。在 System.RuntimeTypeHandle.CreateInstance(RuntimeType 类型,Boolean publicOnly,Boolean noCheck,Boolean& canBeCached,RuntimeMethodHandleInternal& ctor,Boolean& bNeedSecurityCheck)在 System.RuntimeType.CreateInstanceSlow(Boolean publicOnly,Boolean skipCheckThis,Boolean fillCache,StackCrawlMark 和 stackMark)在 System.RuntimeType.CreateInstanceDefaultCtor (布尔 publicOnly,布尔 skipCheckThis,布尔填充缓存,StackCrawlMark 和 stackMark)
在 System.Activator.CreateInstance(类型类型,布尔非公共)
在 System.Activator.CreateInstance(Type type) 在 System.Web.Mvc.DefaultModelBinder.CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType) 在 System.Web.Mvc.DefaultModelBinder.BindSimpleModel(ControllerContext controllerContext, ModelBindingContext bindingContext, ValueProviderResult valueProviderResult)在 System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) 在 System.Web.Mvc.DefaultModelBinder.GetPropertyValue(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, IModelBinder propertyBinder)
在 System.Web.Mvc.DefaultModelBinder.BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor) 在 System.Web.Mvc.DefaultModelBinder.BindProperties(ControllerContext controllerContext, ModelBindingContext bindingContext) 在 System.Web.Mvc.DefaultModelBinder.BindComplexElementalModel(ControllerContext controllerContext) , ModelBindingContext bindingContext, 对象模型)
在 System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) 在 System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) 在 System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor ) 在 System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) 在 System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c_ DisplayClass25.b _1e(AsyncCallback asyncCallback, Object asyncState) 在 System.Web.Mvc。 Async.AsyncResultWrapper.WrappedAsyncResult1.Begin(AsyncCallback callback, Object state, Int32 timeout) at System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) at System.Web.Mvc.Controller.<>c__DisplayClass1d.<BeginExecuteCore>b__17(AsyncCallback asyncCallback, Object asyncState) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult1.开始(AsyncCallback 回调,对象状态,Int32 超时)在 System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback 回调,对象状态)在 System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult1.Begin(AsyncCallback callback, Object state, Int32 timeout) at System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) at System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) at System.Web.Mvc.MvcHandler.<>c__DisplayClass8.<BeginProcessRequest>b__2(AsyncCallback asyncCallback, Object asyncState) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult1.在 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback 回调, 对象状态) 处 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback 回调处的开始(AsyncCallback 回调, 对象状态, Int32 超时),对象状态)在 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) 在 System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 在System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

4

1 回答 1

9

解决此问题的方法是将分页元数据作为单独的属性传递,并在视图中重建 IPagedList。像下面

public class PagedClientViewModel
{
    public int? Page { get; set; }
    public List<ClientViewModel> Clients { get; set; }
    public IPagedList PagingMetaData { get; set; } 
}

生成元数据

pagedClientViewModel.PagingMetaData = new StaticPagedList<ClientViewModel>(pagedClientViewModel.Clients, pageIndex, pageSize, TotalClients).GetMetaData();

在视图中构造寻呼机

<div style="text-align: center">
    @Html.PagedListPager(new StaticPagedList<ClientViewModel>(Model.Clients, Model.PagingMetaData), page => Url.Action("<actionname>", new { page }), PagedListRenderOptions.Classic)
</div>
于 2013-01-19T14:51:27.150 回答