I am using petapoco orm in mvc 4. when i going to show my total client list from database table it showing me object reference not set to and instance of a object. I have also some data or information in my Client Table. Why this error is showing , Please help me... my index view is.
@model IEnumerable<FCBook.Client>
@{
ViewBag.Title = "Client list";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.ClientName)
</th>
<th>
@Html.DisplayNameFor(model => model.ContactName)
</th>
<th>
@Html.DisplayNameFor(model => model.PhoneNo)
</th>
<th>
@Html.DisplayNameFor(model => model.Fax)
</th>
<th>
@Html.DisplayNameFor(model => model.Email)
</th>
<th>
@Html.DisplayNameFor(model => model.Address)
</th>
<th>
@Html.DisplayNameFor(model => model.City)
</th>
<th>
@Html.DisplayNameFor(model => model.PostalCode)
</th>
<th>
@Html.DisplayNameFor(model => model.Country)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.ClientName)
</td>
<td>
@Html.DisplayFor(modelItem => item.ContactName)
</td>
<td>
@Html.DisplayFor(modelItem => item.PhoneNo)
</td>
<td>
@Html.DisplayFor(modelItem => item.Fax)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
@Html.DisplayFor(modelItem => item.Address)
</td>
<td>
@Html.DisplayFor(modelItem => item.City)
</td>
<td>
@Html.DisplayFor(modelItem => item.PostalCode)
</td>
<td>
@Html.DisplayFor(modelItem => item.Country)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ClientId }) |
@Html.ActionLink("Details", "Details", new { id=item.ClientId }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ClientId })
</td>
</tr>
}
</table>
Controller :
public ActionResult Create()
{
return View();
}
//
// POST: /Client/Create
[Httppost]
public ActionResult Create(Client collection)
{
try
{
// TODO: Add insert logic here
var db = new PetaPoco.Database("FCbook");
if (collection != null)
{
collection.Insert();
}
return RedirectToAction("Index");
}
catch
{
return View();
}
}
Controller for your Index
method
public ActionResult Index()
{
var db = new PetaPoco.Database("FCBook");
var Cllist = db.Query<Client>("Select * from Client");
if (Cllist != null)
{
//Convert your Cllist into list of your Model i.e List<FCBook.Client>
return View(Cllist);
}
else
{
return RedirectToAction("");
}
}