部分视图错误未将对象引用设置为对象的实例。 MVC4剃刀

时间:2013-11-18 07:46:33

标签: c# asp.net-mvc-4 razor

我正在做一个项目,为MVC4和Razor的学校创建一个CMS。我需要在索引上创建一个局部视图(最新新闻和工作人员)并为每个模型获取最多5条记录我在我的共享文件夹中创建了一个局部视图,但我一直有这样的错误消息

  

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

Line 1:  @model IEnumerable<MvcDale.Models.StaffModels>
Line 2:  @foreach ( var staff in Model ){
Line 3:      <div class="wrapper p4-1">
Line 4:          <figure class="img-indent3 box1">
  

源文件:c:\ Users \ Hammed \ Documents \ Visual Studio 2010 \ Projects \ MvcDale \ MvcDale \ Views \ Shared_StaffListPartial.cshtml Line:2

这是我的部分视图文件看起来像

@model IEnumerable<MvcDale.Models.StaffModels>
@foreach ( var staff1 in Model ){
    <div class="wrapper p4-1">
        <figure class="img-indent3 box1">
            <img src="@Url.Content(staff1.Picture)" alt="">
        </figure>
        <div class="extra-wrap">
            <h6><a href="#">@staff1.title @staff1.sname @staff1.oname @staff1.fname</a></h6>
            @staff1.profile 
        </div>
    </div>
    }
}

从我的家庭索引视图中,我可以像这样调用它

{
   @Html.Partial("~/Views/Shared/_StaffListPartial.cshtml")
}

有人可以帮助我,或者告诉我我的代码有什么问题。刚刚第一次做ASP.net MVC。感谢

@Dmytro

这是我的staffModel类

using System.Data.Entity;

namespace MvcDale.Models
{
    [Table("Staff")]
    public class StaffModels
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int staff_id { get; set; }
        public string title { get; set; }
        public string fname {get; set;}
        public string sname {get; set;}
        public string oname {get; set;}
        public string phone { get; set; }
        public string email { get; set; }
        public string profile { get; set; }
        public string Picture { get; set; }

    }
}

这是来自〜/ views / home / index

的index.cshtml
@{ Layout = "~/Views/Shared/_LayoutPage1.cshtml"; }


@{
    ViewBag.Title = "Home Page";
    var Content = ViewBag.Content;
}

@section feature {
<div class="grid_11">
<div>
@Content;
</div>

                                <div class="indent-bottom10 indent-top5">
                                    <h2 class="p5"><span class="color-1">We are among</span> the leading research and teaching institutions of the world!</h2>
                                    <div class="wrapper">
                                        <div class="col-3-1">
                                            <h4 class="p2">Education Projects</h4>
                                            <ul class="list-1 p4-1">
                                                <li><a href="#">Education and Jobs</a></li>
                                                <li><a href="#">Public School Facts</a></li>
                                                <li><a href="#">International Studies</a></li>
                                                <li><a href="#">Public Engagement</a></li>
                                                <li class="last-item"><a href="#">State Testing Data</a></li>
                                            </ul>
                                            <a href="#" class="button button1">View All<span></span></a>
                                        </div>
                                        <div class="col-3-1 font-1">
                                            <h4 class="p2">General Info</h4>
                                            <p class="p5-1">Feipsumorbi nunc odiovia suorem aecena stiq cumsan malestonsetue adipiscing elit. Dolor&shy;sedum. Mauris fermen tum did oreealiquam leotum dictum magna. Sed oreet aliquam leo. Ut tellus dolor</p>
                                            <a href="#" class="button">Read More<span></span></a>
                                        </div>
                                        <div class="col-3-1 font-1 last-item">
                                            <h4 class="p2">Partners Programs</h4>
                                            <p class="indent-right4 p5-1">Nunc massa suorena stiq cumalest onsetuer adipi&shy;scing elit. Mauris fermen tum did oreet aliquam leo. Ut tellus dolor dapibuso eget elementum vel curus eleife elit. </p>
                                            <a href="#" class="button">Read More<span></span></a>
                                        </div>
                                    </div>
                                </div>
                                <div class="indent-right12">
                                    <h2 class="p6"><a href="#">Latest News</a></h2>
                                    <div class="wrapper font-1">
                                        <figure class="img-indent img-indent-none3">
                                            <img src="~/Images/teachers.png" alt="" />
                                        </figure>
                                        <div class="indent-top1 extra-wrap extra-wrap-none2 ">
                                            <p class="p2-1">Many surgeons are seriously affected on an emotional level when complications occur in the operating theatre, a study finds&shy;<a href="#" class="button">Read More<span></span></a>
                                        </div>
                                    </div>
                                </div>
                            </div>
}

@section staff_{
<h3 class="p3-1">our staff</h3>
 @model MvcDale.Models.GenModels
    @foreach ( var staff1 in Model.Staff)
    {
        @Html.Partial("~/Views/Shared/_StaffListPartial.cshtml", staff1)

    }                                                                                       

                                <a href="#" class="link">view all</a>

}

因为这是我的职员空间视图,我打算从

生成一个5(最近)的列表
@model MvcDale.Models.StaffModels

    <div class="wrapper p4-1">
        <figure class="img-indent3 box1">
            <img src="@Url.Content(Model.Picture)" alt="">
        </figure>
        <div class="extra-wrap">
            <h6><a href="#">@Model.title @Model.sname @Model.oname @Model.fname</a></h6>
            @Model.profile 
        </div>
    </div>

这是我打算成功的 所有模型和dbcontext都按顺序我可以粗略地使用db表

1 个答案:

答案 0 :(得分:3)

您必须在Html.Partial调用中为您的部分视图指定模型

@Html.Partial("~/Views/Shared/_StaffListPartial.cshtml", Model.StaffModelsList)