gpt4 book ai didi

asp.net-mvc - 我在运行局部 View 时出错

转载 作者:行者123 更新时间:2023-12-02 03:19:38 25 4
gpt4 key购买 nike

您好,我刚刚在 C# 中启动了 MVC 4,我在呈现局部 View 时遇到问题。我已经为部分 View 和 Controller 操作创建了模型,我只是出于测试原因发送单个字符串,但是当我尝试呈现它时。它只是显示以下错误。

[NullReferenceException: Object reference not set to an instance of an object.] here is my controller class.

enter code here

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using my_photos.Models;


namespace my_photos.Controllers
{
public class DefaultController : Controller
{


public ActionResult Index()
{
ViewBag.Massage = "Hello Word";
return View();
}
public ActionResult About() {
ViewBag.Message = "About Page";
return View();
}
public ActionResult _RightView() {
var model = new my_photos.Models.partial(){
Winner = "Shafee Jan"
};
//ViewData["name"] = model;
return View(model);
}
}

这里是局部 View 的模型类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace my_photos.Models
{
public class partial
{

public string Winner { get; set; }
}
}

这是我的部分观点

@model my_photos.Models.partial

<div class="body">
<div class="content">
<div class="header">
<h1 class="nav-heading">Data</h1>
<p class="paragraph" > Winner :@Model.Winner.ToString() </p>
</div>
</div>
<div class="bottom">
<div class="header">
</div>
</div>
</div>

这是我在共享文件夹中的主要布局

@model my_photos.Models.partial
@{
Layout = null;
}
<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>_Layout</title>
<link href="@Url.Content("~/Content/Style/Site.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Content/Style/Side_barnav.css")" rel="stylesheet" type="text/css" />


</head>
<body>
<header>
<h3> Photos app</h3>
<p> In this app my major concerns are with the <strong>theaming</strong>!</p>
<div class="nav">
<ul class="main-menu">
<li>@Html.ActionLink("Home","Index", "Default")</li>
<li>@Html.ActionLink("About","About", "Default")</li>
<li>@Html.ActionLink("Contact","Contact", "Default")</li>
</ul>
</div>
</header>
<section>
@RenderBody()
@Html.Partial("~/Views/Shared/_partial.cshtml")
</section>
<footer>
<div class="fotter-menu">
<ul>
<li>@Html.ActionLink("Home","Index","Default")</li>
<li>@Html.ActionLink("About","About","Default")</li>
<li>@Html.ActionLink("Contact","Contact","Default")</li>
</ul>
<ul>
<li>@Html.ActionLink("Resources","Resources","Default")</li>
<li>@Html.ActionLink("Testimonial","Testimonial","Default")</li>
<li>@Html.ActionLink("Team","Team","Default")</li>
</ul>
<ul>
<li>@Html.ActionLink("Home","Index","Default")</li>
<li>@Html.ActionLink("Home","Index","Default")</li>
<li>@Html.ActionLink("Home","Index","Default")</li>
</ul>
</div>
</footer>
</body>
</html>

每当我尝试从模型中获取值时,它都会给我以下错误。请帮助我解决这个问题。

[NullReferenceException: Object reference not set to an instance of an object.]

最佳答案

发生这种情况是因为您的模型是 null,如您所见,在操作 AboutIndex 中没有模型被传递。还有 partial是C#中的关键字,取个更有意义的名字会更好。

要解决这个问题,您必须将一个模型传递给每个使用期望该模型的布局的 View 。在您的情况下是 null 并引发错误。

public ActionResult Index()
{
ViewBag.Massage = "Hello Word";
var model = new my_photos.Models.partial(){
Winner = "Shafee Jan"
};
return View(model);
}

传递模型是必要的,因为在下面的代码行中,部分由当前 View 模型默认呈现。

@Html.Partial("_partial.cshtml", Model) @* Model is passed by default if there is no other parameter specified *@

我认为您真正想要做的是在您的布局中调用 _RightView 操作。

@Html.Action("_RightView", "Default")

不要忘记修改操作以返回部分 View ,并且不需要在其他操作中传递模型

public ActionResult  _RightView() {
var model = new my_photos.Models.partial(){
Winner = "Shafee Jan"
};
//ViewData["name"] = model;
return PartialView("_partial", model);
}

关于asp.net-mvc - 我在运行局部 View 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34329848/

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