0

我正在尝试运行我的 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
4

2 回答 2

0

也许这是因为您从 catch 块返回“View(model:ex)”。请调试您的代码并检查引发异常的原因。

于 2018-01-26T21:05:11.550 回答
0

您正在尝试将异常传递给您的视图:

return View(model:ex);

但是,正如错误所述,视图不会出现异常。它期待一个特定的模型:

@model Mytest.Models.ProductsListViewModel

所以看起来处理错误的尝试本身就是导致错误的原因。那么问题就真的变成了:

如何处理错误?

您真的要返回视图但没有任何数据吗?是否要重定向到错误页面?以某种方式记录错误?还有什么?

最终,您需要具体决定要做什么来处理错误。您甚至可以依靠框架为您定向到错误页面,而根本不用自己处理它。(这里有多种选项可供您使用,Google 搜索“ASP .NET MVC 错误处理”会产生许多文章和示例。)

然后,一旦你有意义地处理错误,你就成功了一半。此时您首先需要找出该错误是什么并开始纠正错误。

于 2018-01-26T21:09:56.860 回答