我正在尝试运行我的 ASP.NET MVC 应用程序,但它没有运行,请您让我了解我做错了什么,谢谢。模型
namespace Mytest.Models {
public class ProductsListViewMode {
public IEnumerable<Product> Products { get; set; }
public PagingInfo PagingInfo { get; set; }
public string CurrentCategory { get; set; }
}
}
控制器
public ViewResult List(string categoryStr,int page = 1)
{
try
{
ProductsListViewModel model = new ProductsListViewModel{
Products = repository.Products
.Where(p => categoryStr == null || p.CategoryInfo == categoryStr)
.OrderBy(p => p.ProductID)
.Skip((page - 1) * PageSize)
.Take(PageSize),
PagingInfo = new PagingInfo
{
CurrentPage = page,
ItemsPerPage = PageSize,
TotalItems = categoryStr == null ?
repository.Products.Count() :
repository.Products.Where(c=>c.CategoryInfo==categoryStr).Count()
},
CurrentCategory = categoryStr
};
return View(model);//These change
}
catch(InvalidOperationException ex)
{
return View(model:ex);
}
}
和@view
@model Mytest.Models.ProductsListViewModel
@{
ViewBag.Title = "Products";
}
@foreach (var p in Model.Products)
{
Html.RenderPartial("ProductSummary",p);
}<hr/>
<div class="pager">
@Html.PageLinks(Model.PagingInfo, x=>Url.Action("List", new{page=x, categoryStr=Model.CurrentCategory}))
</div>
这些是错误消息
[InvalidOperationException: The model item passed into the dictionary is of type 'System.InvalidOperationException', but this dictionary requires a model item of type 'LeoSaFashion.Models.ProductsListViewModel'.]
System.Web.Mvc.ViewDataDictionary`1.SetModel(Object value)+175
System.Web.Mvc.ViewDataDictionary..ctor(ViewDataDictionary dictionary)+107
System.Web.Mvc.WebViewPage`1.SetViewData(ViewDataDictionary viewData)+49
System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)+99
System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)+107
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)+291
System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)+13
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)+56
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)+420
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)+52
System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c()+173
System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult)+100
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult)+10 System.Web.Mvc.Async.WrappedAsyncResultBase`1.End()+49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult)+27
System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState)+13
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)+29
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End()+49
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)+36
System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller)+12
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)+22
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End()+49
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult)+26
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult)+10
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState)+21
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)+29
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End()+49
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)+28
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)+9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+9748493 System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step)+48 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)+159