我在部分视图中遇到错误

时间:2015-12-17 08:27:02

标签: asp.net-mvc asp.net-mvc-4

您好我刚刚在c#中启动了MVC 4,我在渲染局部视图时遇到了问题。 我为部分视图和控制器动作创建了模型 出于测试原因发送单个字符串,但是当我尝试渲染它时。 它只显示以下错误。

  

[NullReferenceException:对象引用未设置为对象的实例。]   这是我的控制器类。

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);
        }
    }

这是部分视图的模型类

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:对象引用未设置为对象的实例。]

2 个答案:

答案 0 :(得分:1)

这种情况正在发生,因为您的模型为null,正如您所见,在操作AboutIndex中,没有传递任何模型。另外partial是C#中的关键字,最好有一个更有意义的名称。

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

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

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

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

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

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

不要忘记修改动作以返回局部视图,并且不需要在其他动作中传递模型

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

答案 1 :(得分:0)

嘿,这对于布局和局部视图的部分类引用并不是一个大问题。

因此,如果您访问_RightView Action它不会抛出错误,因为您正在传递一个对象以正确查看但是当它到达部分视图时您没有传递对象引用因此只需传递模型

@ Html.Partial( “〜/查看/共享/ _partial.cshtml”,模型)

就是这样