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 }
我想将模型发布回控制器,以便我可以处理已勾选的复选框,但是我收到以下错误:我有点理解错误是因为我回发了一个界面但我找不到方法在那附近.我怎样才能完成这项工作?
Cannot create an instance of an interface. at
System.RuntimeTypeHandle.CreateInstance(RuntimeType type,Boolean
publicOnly,Boolean noCheck,Boolean& canBeCached,
RuntimeMethodHandleInternal& ctor,Boolean& bNeedSecurityCheck) at
System.RuntimeType.CreateInstanceSlow(Boolean publicOnly,Boolean
skipCheckThis,Boolean fillCache,StackCrawlMark& stackMark) at
System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly,
Boolean skipCheckThis,StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type,Boolean nonPublic)
at System.Activator.CreateInstance(Type type) at
System.Web.Mvc.DefaultModelBinder.CreateModel(ControllerContext
controllerContext,ModelBindingContext bindingContext,Type modelType)
at System.Web.Mvc.DefaultModelBinder.BindSimpleModel(ControllerContext
controllerContext,
ValueProviderResult valueProviderResult) at
System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext
controllerContext,ModelBindingContext bindingContext) at
System.Web.Mvc.DefaultModelBinder.GetPropertyValue(ControllerContext
controllerContext,
PropertyDescriptor propertyDescriptor,IModelBinder propertyBinder)
at System.Web.Mvc.DefaultModelBinder.BindProperty(ControllerContext
controllerContext,
PropertyDescriptor propertyDescriptor) at
System.Web.Mvc.DefaultModelBinder.BindProperties(ControllerContext
controllerContext,ModelBindingContext bindingContext) at
System.Web.Mvc.DefaultModelBinder.BindComplexElementalModel(ControllerContext
controllerContext,Object model)
at
System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext
controllerContext,ModelBindingContext bindingContext) at
System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext
controllerContext,ModelBindingContext bindingContext) at
System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext
controllerContext,ParameterDescriptor parameterDescriptor) at
System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext
controllerContext,ActionDescriptor actionDescriptor) at
System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c_DisplayClass25.b_1e(AsyncCallback
asyncCallback,Object asyncState) at
System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult1.Begin(AsyncCallback
1.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.WrappedAsyncResult
callback,Int32 timeout) at
System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback,
Object state) at
System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult1.Begin(AsyncCallback
callback,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,Object state) at
System.Web.Mvc.MvcHandler.<>c__DisplayClass8.<BeginProcessRequest>b__2(AsyncCallback
asyncCallback,Int32 timeout) at
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase
httpContext,Object state) at
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext,Object state) at
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext
context,AsyncCallback cb,Object extraData) at
System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step,
Boolean& completedSynchronously)
解决方法
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>