Tahir Mehmet
Tahir Mehmet

Reputation: 149

ASP.Net MVC4 Cannot perform runtime binding on a null reference

I am having this issue that keeps flagging up and I have checked every file. Just want to see if anyone else can spot an error?

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

All reply's are much appreciated !

Bellow is the Event Model which is linked to the orginal post:

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

}
}

See bellow edited I am getting a error it says it does not contain a definition for the following Date, Town, Country and Report :

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

}
}

Upvotes: 2

Views: 4838

Answers (1)

user3559349
user3559349

Reputation:

You have not declared you model in the view. At the top of the view you need to declare it

@model NETCW.Models.Event

Edit

Based on the edited question @Model.item.eventname will also fail because Event does not contain a property named item. In addition you need to pass the model to the view, and you should be using the htlm helpers to generate the elements. In the GET method

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

and in the view

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

and if your property is decorated with [Display(Name = "Event Name:")] then you can use the following to generate the associated label

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

Upvotes: 3

Related Questions