gpt4 book ai didi

asp.net-mvc - MVC 发布 IPgedList

转载 作者:行者123 更新时间:2023-12-02 01:37:37 25 4
gpt4 key购买 nike

我有以下 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)
}

Controller 操作:

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

我想将模型发布回 Controller ,以便我可以处理已勾选的复选框,但出现以下错误:我明白该错误是因为我正在回发接口(interface),但我不能找到解决办法。我怎样才能得到这份工作?

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, Boolean fillCache, 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, ModelBindingContext bindingContext, ValueProviderResult valueProviderResult) at System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) at System.Web.Mvc.DefaultModelBinder.GetPropertyValue(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, IModelBinder propertyBinder)
at System.Web.Mvc.DefaultModelBinder.BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor) at System.Web.Mvc.DefaultModelBinder.BindProperties(ControllerContext controllerContext, ModelBindingContext bindingContext) at System.Web.Mvc.DefaultModelBinder.BindComplexElementalModel(ControllerContext controllerContext, ModelBindingContext bindingContext, 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
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
1.Begin(AsyncCallback callback, Object state, Int32 timeout) at System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) at 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.WrappedAsyncResult
1.Begin(AsyncCallback callback, Object state, Int32 timeout) at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, 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)

最佳答案

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

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();

在 View 中构造寻呼机

<div style="text-align: center">
@Html.PagedListPager(new StaticPagedList<ClientViewModel>(Model.Clients, Model.PagingMetaData), page => Url.Action("<actionname>", new { page }), PagedListRenderOptions.Classic)
</div>

关于asp.net-mvc - MVC 发布 IPgedList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14258212/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com