首页 新闻 会员 周边

异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。

0
悬赏园豆:10 [已解决问题] 解决于 2018-01-22 11:37

写了一个asp.net MVC的东西,再报这个错,本人小白,求大神解惑

“/”应用程序中的服务器错误。

未将对象引用设置到对象的实例。 
说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 

异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。

源错误: 


行 24:     </tr>25: 
行 26: @foreach (var item in Model) {
行 27:     <tr>28:         <td>

源文件: F:\实习\jhcdss\CDSS.Console\Views\RuleClassify\Index.cshtml    行: 26 

堆栈跟踪: 


[NullReferenceException: 未将对象引用设置到对象的实例。]
   ASP._Page_Views_RuleClassify_Index_cshtml.Execute() in F:\实习\jhcdss\CDSS.Console\Views\RuleClassify\Index.cshtml:26
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +197
   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +105
   System.Web.WebPages.StartPage.RunPage() +17
   System.Web.WebPages.StartPage.ExecutePageHierarchy() +64
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +78
   System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +235
   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

出错的代码

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.RuleName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.RuleDetails)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Enable)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.RuleName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.RuleDetails)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Enable)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
            @Html.ActionLink("Details", "Details", new { id=item.ID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.ID })
        </td>
    </tr>
}

控制器的代码

 public class RuleClassifyController : Controller
    {
        private RuleClassifyContext ruleclassifyContext = new RuleClassifyContext();
        // GET: Ruleclassification
        public ActionResult Index()
        {
            return View(ruleclassifyContext.ruleclassify);
        }

模型代码

    public class RuleClassify
    {
        public int ID { get; set; }
        public string RuleName { get; set; }
        public string RuleDetails { get; set; }
        public int Enable { get; set; }
    }
    public class RuleclassificationList
    {
        public List<RuleClassify> ruleclassifyList { get; set; }
    }
}
namespace CDSS.DataContext
{
    public class RuleClassifyContext
    {
        public IMongoCollection<RuleClassify> ruleclassify { get; set; }
    }
}
xingcs的主页 xingcs | 初学一级 | 园豆:4
提问于:2018-01-19 16:58
< >
分享
最佳答案
1

你创建的上下文里还差东西吧

收获园豆:10
ll...ll | 菜鸟二级 |园豆:233 | 2018-01-19 17:12
其他回答(1)
0

Model 的值为 null

dudu | 园豆:30994 (高人七级) | 2018-01-19 17:15

为什么为null啊,该怎么改啊

支持(0) 反对(0) xingcs | 园豆:4 (初学一级) | 2018-01-22 09:30
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册