对象引用未设置为对象的实例在视图中使用foreach循环时。 [英] Object reference not set to an instance of an object While using foreach loop in a View.

查看:137
本文介绍了对象引用未设置为对象的实例在视图中使用foreach循环时。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在创建一个View并在其中调用Partial View,如下所示



 @ {
//ViewBag.Title =;
Layout =〜/ Views / Shared / _restLayout.cshtml;
}


< div class =row sidebar_left>
< div class =column_center>
< div id =main_contentclass =col-sm-9>
@RenderBody()
< / div>
< / div>

< div class =column_left column col-sm-3>
< div class =widget widget__collections>
< h3 class =widget_header>收藏品< / h3>
@ Html.Partial(categoryPartial)
< / div>
< / div>
< / div>





当我运行应用程序时,它会抛出错误对象引用未设置为实例一个对象在foreach循环中。





 @model IEnumerable< fashionHub_new.Models.category> 

< div class =widget_content>
< ul class =list>
@foreach(ViewData [Category]中的var项目为List< fashionHub_new.Models.category>)
{
< li class =underwear-socks>
< a href =#title =ABC> ABC< / a>
< / li>
}
< / ul>
< / div>





请在下方找到我的控制器



 public ActionResult Index()
{
ViewData [Category] = _businessLayer.ViewAllCategory()。ToString();
return View();
}





请提供相同的解决方案。

解决方案

 ViewData [Category] = _businessLayer.ViewAllCategory()。ToString(); 





ViewData [Category]被设置为一个字符串。



 ViewData [Category]如List< fashionHub_new.Models.category>) 





as尝试将ViewData [Category](字符串)转换为List< category>如果不能,则返回null。由于您的ViewData是一个字符串,它将返回null。您可能需要在控制器中执行此操作



 ViewData [Category] = _businessLayer.ViewAllCategory()。ToList(); 


Hi I am creating a View and calling Partial View in it as below

@{
    //ViewBag.Title = "";
    Layout = "~/Views/Shared/_restLayout.cshtml";
}


<div class="row sidebar_left ">
    <div class="column_center">
        <div id="main_content" class="col-sm-9">
            @RenderBody()
        </div>
    </div>

    <div class="column_left column col-sm-3">
        <div class="widget widget__collections">
            <h3 class="widget_header">Collections</h3>
            @Html.Partial("categoryPartial")
        </div>
    </div>
</div>



When I run the application it throws an error "Object reference not set to an instance of an object " in foreach loop.


@model IEnumerable<fashionHub_new.Models.category>

<div class="widget_content">
    <ul class="list">
        @foreach (var item in ViewData["Category"] as List<fashionHub_new.Models.category>)
        {
            <li class="underwear-socks">
                <a href="#" title="ABC">ABC</a>
            </li>
        }
    </ul>
</div>



Please find my controller below

public ActionResult Index()
        {
            ViewData["Category"] = _businessLayer.ViewAllCategory().ToString();
            return View();
        }



Please provide the solution for the same.

解决方案

ViewData["Category"] = _businessLayer.ViewAllCategory().ToString();



ViewData["Category"] is being set to a string.

ViewData["Category"] as List<fashionHub_new.Models.category>)



the "as" tries to convert ViewData["Category"] (a string) as a List<category> and if it can't it returns null. As your ViewData is a string it will return null. You probably need to do this instead in your controller

ViewData["Category"] = _businessLayer.ViewAllCategory().ToList();


这篇关于对象引用未设置为对象的实例在视图中使用foreach循环时。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆