Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI All,

In my controllers folder I got an action list method that returns a book object like this:

C#
public ActionResult Test()
       {
           Book book = new Book()
           {

               ID = 1,
               BookName = "Abraham Life",
               AuthorName = "King",
               ISBN = "NA"
           };
           return View();

       }


when I run the project i keep getting a null exception error in the Test view file for this action Test method.

To be more specific the error comes from the actionlink like:
HTML
<p>
    @Html.ActionLink("Edit", "Edit", new { id = Model.ID }) |
    @Html.ActionLink("Back to List", "Index")
</p>


and when I hover my mouse pointer on the second edit it says cannot resolve action Edit.
is there anything Im doing wrong?? some please help.



and my model folder I have defined the properties like so:

C#
public class Book
   {
       public int ID { get; set; }
       public string BookName { get; set; }
       public string AuthorName { get; set; }
       public string ISBN { get; set; }
   }
Posted

1 solution

You're not passing an object to the View.

In your Index action, you do this by passing the object you want to pass to the view like this:
return View(Book);


BUT! Normally you don't pass entity objects to the view. You pass a view model class instance.

Why? Because passing a view model you can also pass in other information that the view may need, like values for dropdowns or other UI or state information, that the entity object cannot contain.
 
Share this answer
 
Comments
1Future 27-Jul-14 12:04pm    
oh yeah! idiot me! Thank you soo very much

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900