简体   繁体   English

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

[英]Partial view error Object reference not set to an instance of an object. MVC4 Razor

i am doing a project to create a CMS for a school with MVC4 and Razor. 我正在做一个项目,为MVC4和Razor的学校创建一个CMS。 I need to create a partial view on the index (for latest news and staff) and get at most 5 records for each of these Models i created a partial view in my shared folder but i kept having error message like this 我需要在索引上创建一个局部视图(对于最新的新闻和工作人员),并为每个模型获取最多5条记录我在我的共享文件夹中创建了一个局部视图,但我一直有这样的错误消息

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. 异常详细信息: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">

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

this is what my partial view file is looking like 这是我的部分视图文件看起来像

@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>
    }
}

And from my home index view i have the call to it like this 从我的家庭索引视图,我有这样的呼吁

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

can somebody help me out or suggest to me what is wrong with my code. 有人可以帮助我或建议我的代码有什么问题。 just doing ASP.net MVC for the first time. 刚刚第一次做ASP.net MVC。 thanks 谢谢

@Dmytro @Dmytro

this my staffModel class 这是我的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; }

    }
}

this is my index.cshtml from ~/views/home/index 这是来自〜/ 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>

}

as this is my staff patial view i intend in generating a list of 5(atmost) from 因为这是我的员工patial视图,我打算生成一个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>

This is what i intend to archieve all model and dbcontext are in order i can crude the db table already 这是我打算推荐所有模型和dbcontext以便我可以粗略地使用db表

你必须在像这样的Html.Partial调用中为你的局部视图指定模型

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MVC4部分视图错误“对象引用未设置为对象的实例。” - MVC4 Partial View Error “Object reference not set to an instance of an object.” 你调用的对象是空的。 MVC - object reference not set to an instance of an object. MVC 在mvc项目中插入错误:{“对象引用未设置为对象的实例。”} - Insertion in mvc project error: {“Object reference not set to an instance of an object.”} 错误“对象引用未设置为对象的实例。” - Error 'Object reference not set to an instance of an object.' 你调用的对象是空的。 错误 - Object reference not set to an instance of an object. Error 你调用的对象是空的。 错误 - Object reference not set to an instance of an object. Error MVC Razor 对象引用未设置为对象的实例 - MVC Razor Object reference not set to an instance of an object Object 引用未设置为 object 的实例。 MVC 和外键 - Object reference not set to an instance of an object. MVC and Foreign Key 你调用的对象是空的。 如何查看堆栈 - Object reference not set to an instance of an object. How to view the stack MVC:通过SelectListItem,我得到“ System.NullReferenceException:对象引用未设置为对象的实例。”错误 - MVC: By SelectListItem I get “System.NullReferenceException: Object reference not set to an instance of an object.” error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM