Html.Action throwing exception: An exception of type 'System.Web.HttpException' occurred in System.Web.dll

核能气质少年 提交于 2019-12-12 06:19:34

问题


I'm getting this error from @Html.Action("getCategory", "Blogs") in the master layout for my blog.

The error is:

An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user code Additional information: Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'.

Here is my controller:

public ActionResult getCategory()
    {
        var categories = _repo.getBlogs().Select(c => new
        {
            c.Category
        }).ToList();
        return PartialView("_category", categories);
    }

And here is my partial view:

@model  IEnumerable<MainSite.Data.Blog>

<div class="sw_categories">
<div class="sw_title">
    <h4>Categories</h4>
</div>
<ul class="arrows_list">
    @foreach (var c in Model)
    {
        <li><a href="#">@c</a></li>
    }
</ul>
</div>

I'm pretty new to ASP.NET MVC, so could anyone please explain the error to me and how I could fix this?


回答1:


You'll want to either..

  1. return a collection of Blogs from getCategory(), since that is what your partial is expecting:

    IEnumerable<MainSite.Data.Blog>  
    
  2. or change the model type in the partial to correspond with what you're returning from getCategory(). Example:

    IEnumerable<MainSite.Data.Blog.Category> 
    


来源:https://stackoverflow.com/questions/30069459/html-action-throwing-exception-an-exception-of-type-system-web-httpexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!