ASP.Net MVC4无法对空引用执行运行时绑定

时间:2015-04-14 01:00:29

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

我遇到了这个持续标记的问题,我检查了每个文件。只是想看看是否有其他人可以发现错误?

@{
ViewBag.Title = "Event List";
}

<div>

Event Name : @Model.item.eventname <br />
Event Date : @Model.item.date <br />
Event Town : @Model.item.town <br />
Event Country : @Model.item.country <br />
Event Description : @Model.item.description <br />
Event Report : @Model.item.report <br />

@Html.ActionLink("Add New Event" , "AddEvent")


 @*Html.Partial("EventList")*@

</div>

非常感谢所有回复!

Bellow是与原始帖子相关联的事件模型:

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

namespace NETCW.Models
{
public class Event
{
    public int EventID { get; set; }

    [Required(ErrorMessage = "Please enter the event name.")]
    public string eventname { get; set; }

    [Required(ErrorMessage = "Please enter the event date.")]
    public string date { get; set; }

    [Required(ErrorMessage = "Please enter the event town.")]
    public string town { get; set; }

    [Required(ErrorMessage = "Please enter the event country.")]
    public string country { get; set; }

    [Required(ErrorMessage = "Please enter the event description.")]
    public string description { get; set; }

    [Required(ErrorMessage = "Please enter the event report.")]
    public string report { get; set; }

}
}

见bellow编辑我收到一个错误,它说它不包含以下日期,城镇,国家和报告的定义:

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

namespace NETCW.Models
{
public class Event
{
    public int EventID { get; set; }

    [Required(ErrorMessage = "Please enter the event name.")]
    [Display (Name = "Event Name :")]
    public string eventname { get; set; }

    [Required(ErrorMessage = "Please enter the event date.")]
    [Display (Date = "Event Date :")]
    public string date { get; set; }

    [Required(ErrorMessage = "Please enter the event town.")]
    [Display (Town = "Event Town :")]
    public string town { get; set; }

    [Required(ErrorMessage = "Please enter the event country.")]
    [Display (Country = "Event Country :")]
    public string country { get; set; }

    [Required(ErrorMessage = "Please enter the event description.")]
    [Display (Description = "Event Description :")]
    public string description { get; set; }

    [Required(ErrorMessage = "Please enter the event report.")]
    [Display (Report = "Event Report :")]
    public string report { get; set; }

}
}

1 个答案:

答案 0 :(得分:3)

您尚未在视图中声明模型。在视图的顶部,您需要声明它

@model NETCW.Models.Event

修改

根据已编辑的问题@Model.item.eventname也会失败,因为Event不包含名为item的属性。此外,您需要将模型传递给视图,并且您应该使用htlm帮助程序来生成元素。在GET方法

Event model = new Event();
return View(model);

并在视图中

@Html.DisplayFor(m => m.eventname)

如果您的财产使用[Display(Name = "Event Name:")]进行装饰,那么您可以使用以下内容生成相关标签

@Html.DisplayNameFor(m => m.eventname)