Reputation: 1
I am trying to run my ASP.NET MVC application but it is not running would you be kind to let me understand what I did wrong, thanks. Model
namespace Mytest.Models {
public class ProductsListViewMode {
public IEnumerable<Product> Products { get; set; }
public PagingInfo PagingInfo { get; set; }
public string CurrentCategory { get; set; }
}
}
Controller
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);
}
}
and @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>
These are the error messages
[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
Upvotes: 0
Views: 123
Reputation: 218847
You're trying to pass an exception to your view:
return View(model:ex);
But, as the error states, the view isn't expecting an exception. It's expecting a specific model:
@model Mytest.Models.ProductsListViewModel
So it appears that an attempt to handle an error is itself causing an error. The question then really becomes:
How do you want to handle an error?
Do you really want to return the view but without any data? Do you want to redirect to an error page? Log the error in some way? Something else?
Ultimately you need to decide specifically what you want to do in order to handle an error. You can even rely on the framework to direct to an error page for you and simply not handle it yourself at all. (There are a variety of options available to you here, a Google search for "ASP .NET MVC error handling" will yield many articles and examples.)
Then, once you are meaningfully handling the error, you're half-way there. At that point you need to find out what that error was in the first place and begin to correct that error.
Upvotes: 0
Reputation: 11
Maybe this is because you return "View(model:ex)" from your catch block. Please debug your code and check why an exception is thrown.
Upvotes: 0