Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hii friends

i am working datatables in mvc. in this i am getting some errors please help me friends.

i am executing List page

i am getting
C#
Object reference not set to an instance of an object

in Viewpage at @ForEach lop like

C#
@foreach (var item in Model.Venues_Filter)
{
    ________
}



please help me friends

What I have tried:

Controler..

C#
public ActionResult DataTable(Home obj)
        {
            obj.Venues_Filter = obj.VenueFilterList();
            return Json(new { data = obj }, JsonRequestBehavior.AllowGet);
        }



C#
public ActionResult List()
        {           
            return View();
        }


View..

<head>
<style>
tr.even {
background-color: #F5F5F5 !important;
}
</style>
<link href="//cdn.datatables.net/1.10.9/css/jquery.dataTables.min.css" rel="stylesheet" />
<script src="//cdn.datatables.net/1.10.9/js/jquery.dataTables.min.js"></script>

@section Scripts{
<script src="//cdn.datatables.net/1.10.9/js/jquery.dataTables.min.js"></script>
<script>
$(document).ready(function () {
$('#myTable').DataTable({
"ajax": {
// "url": "/Home/DataTable",
"url": "@Url.Action("DataTable")",
"type": "GET",
"datatype": "json"
},
"columns": [
{ "data": "images", "autoWidth": true },
]
});
});
</script>
}


</head>




HTML
@foreach (var item in Model.Venues_Filter)
{
    <div>
                            <table id="myTable">
                                <thead>
                                    <tr>
                                        <th>Images</th>

                                    </tr>
                                </thead>
                                <tbody>
                                    <tr>
                                        <td>
                                            <img src='http://myeventvenues.elegantsoftsol.com/@Html.DisplayFor(modelItem => item.Venueimages)' width="308" height="163" />
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                            <div class="carousel-caption"> </div>
                        </div>
                        <div class="item">
                            <img src='http://myeventvenues.elegantsoftsol.com/@Html.DisplayFor(modelItem =>item.Venueimages)' width="308" height="163" />
                            <div class="carousel-caption"> </div>
                        </div>
}
Posted
Updated 23-Aug-16 0:14am
v2
Comments
Vincent Maverick Durano 22-Aug-16 11:55am    
If you are fetching data from an AJAX call then why are you using Model.Venues_Filter? You are getting "object not set to an instance" error because you are referencing an object (Model.Venues_Filter) but you didn't declare the model reference in your View.
Member 11652153 23-Aug-16 0:08am    
how to rectify this problem. please give me any suggestion
Vincent Maverick Durano 23-Aug-16 6:15am    
take a look at the solution i've posted.
Member 11652153 23-Aug-16 6:58am    
Hi @Vincent Maverick Durano...u mentioned in ur previous Answer like "@model YourNamespace.SomeFolder.Home;"

Could you please guide me...what is the "SomeFolder" Word.
Vincent Maverick Durano 23-Aug-16 7:04am    
it's just a sample reference. If your Models/ViewModels are stored within a separate folder in your application, and your class namespacing follows your app folder structure then you should use that as a reference.

Please take a look at the link I've provided to know more about it.

1 solution

Check out this article on integrating jQuery DataTable in ASP.NET MVC: http://www.dotnetawesome.com/2015/11/implement-jquery-datatable-in-aspnet-mvc.html[^]

If you want to get data from your Model to your View then you would need to reference the model in your View. For example:

If you have this Action method in your Controller:

C#
public ActionResult YourViewName(Home obj) {
        obj.Venues_Filter = obj.VenueFilterList();
	return View(obj);
}


to get the data from your Home object, you need to define something like this in your View:

C#
@model YourNamespace.SomeFolder.Home;


You may need to change the reference above to where you declared the Home object.

If you are new to ASP.NET MV, then I would really recommend you to start at basic first to understand how things will work in MVC. ASP.NET MVC 5: Building Your First Web Application - Part 1[^]
 
Share this answer
 

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