gpt4 book ai didi

c# - ASP.NET MVC C# : Object reference errors when going to view record details

转载 作者:太空宇宙 更新时间:2023-11-03 11:54:07 25 4
gpt4 key购买 nike

我是 .NET 和 MVC 的新手,在 ASP 受苦太久后第一次学习它,现在是时候转变过来,让我构建 Web 应用程序的工作变得更容易了。

我一直在浏览 Stephen Walther 的有用视频教程来了解大多数事情,到目前为止我取得了很好的进步。我遇到困难的地方是在我的应用程序 ConversationApp 中为记录创建详细信息页面。列出数据和插入新数据工作正常,但每次我去查看任何特定记录的详细信息页面时,我都会收到错误消息“对象引用未设置到对象的实例”。

传递给 Controller ​​的 URL 是:/Home/Details/X(其中 X 是记录的唯一 ID)

如果能帮助我克服可能是我犯的非常愚蠢的错误或疏忽,我将不胜感激。

这是我到目前为止的代码明智的地方:

HomeController.CS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using ConversationApp.Models;

namespace ConversationApp.Controllers
{
public class HomeController : Controller
{

private conversationDBEntities _entities = new conversationDBEntities();

//
// GET: /Home/

public ActionResult Index()
{
return View(_entities.conversation.ToList());
}

//
// GET: /Home/Details/5

public ActionResult Details(int id)
{

return View();
}

//
// GET: /Home/Create

public ActionResult Create()
{
return View();
}

//
// POST: /Home/Create

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Exclude="conversation_id")]conversation conversationToCreate)
{
try
{
// TODO: Add insert logic here
_entities.AddToconversation(conversationToCreate);
_entities.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}

//
// GET: /Home/Edit/5

public ActionResult Edit(int id)
{
return View();
}

//
// POST: /Home/Edit/5

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, FormCollection collection)
{
try
{
// TODO: Add update logic here

return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}

详细信息.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<ConversationApp.Models.conversation>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Details
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<h2>Details</h2>

<fieldset>
<legend>Fields</legend>
<p>
conversation_name:
<%= Html.Encode(Model.conversation_name) %>
</p>
<p>
conversation_owner:
<%= Html.Encode(Model.conversation_owner) %>
</p>
<p>
conversation_description:
<%= Html.Encode(Model.conversation_description) %>
</p>
<p>
conversation_homeurl:
<%= Html.Encode(Model.conversation_homeurl) %>
</p>
</fieldset>
<p>

<%=Html.ActionLink("Edit", "Edit", new { id=Model.conversation_id }) %> |
<%=Html.ActionLink("Back to List", "Index") %>
</p>

/Home/Details/X 的错误消息(其中 X 是记录的唯一 ID)

Server Error in '/' Application.
--------------------------------------------------------------------------------

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 13: <p>
Line 14: conversation_name:
Line 15: <%= Html.Encode(Model.conversation_name) %>
Line 16: </p>
Line 17: <p>

Source File: c:\Users\Michael Harris\Documents\Visual Studio 2008\Projects\ConversationApp\ConversationApp\Views\Home\Details.aspx Line: 15

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
ASP.views_home_details_aspx.__RenderContent2(HtmlTextWriter __w, Control parameterContainer) in c:\Users\Michael Harris\Documents\Visual Studio 2008\Projects\ConversationApp\ConversationApp\Views\Home\Details.aspx:15
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +256
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
System.Web.UI.Control.Render(HtmlTextWriter writer) +10
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
ASP.views_shared_site_master.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in c:\Users\Michael Harris\Documents\Visual Studio 2008\Projects\ConversationApp\ConversationApp\Views\Shared\Site.Master:26
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +256
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
System.Web.UI.Control.Render(HtmlTextWriter writer) +10
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +134
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
System.Web.UI.Page.Render(HtmlTextWriter writer) +29
System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) +59
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1266

--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.4918; ASP.NET
Version:2.0.50727.4918

最佳答案

在 Controller 上的 Details 操作方法中,您没有将模型传递回 View 。

public ActionResult Details(int id)
{
// get model object

return View(//pass model object to view here);
}

关于c# - ASP.NET MVC C# : Object reference errors when going to view record details,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1210413/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com