Click here to Skip to main content
15,921,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In MVC5 I added NuGet PagedList.Mvc and placed using PagedList; above.

I am having problems with two things:
first, I am getting the NullException from line;
C#
return View(edit.thistable.ToPagedList(pageNumber, pageSize));

second, not able to see if the paging is working.

Error code:
An exception of type 'System.ArgumentNullException' occurred in System.Core.dll but was not handled in user code

HomeController.cs
C#
public class HomeController : Controller
{
    private testContext db = new testContext();
    private EditModel edit = new EditModel();
    public ActionResult Index(int? page)
    {
        try
        {
            edit.thistable = db.Data1.ToList();
        }
        catch (Exception ex)
        {
            edit.errorcode = ex.ToString();
        }
        int pageSize = 3;
        int pageNumber = (page ?? 1);
        return View(edit.thistable.ToPagedList(pageNumber, pageSize));
    }

Index.cshtml
HTML
@model  List<EditTables.Models.Table1>

<center><table>
@foreach (var item in Model) {
    <tr>
        <td>
            @Html.TextBoxFor(modelItem => item.Idt)
        </td>
        <td>
            @Html.TextBoxFor(modelItem => item.datetime0)
        </td>
        <td>
            @Html.TextBoxFor(modelItem => item.col1)
        </td>
        <td>
            @Html.TextBoxFor(modelItem => item.col2)
        </td>
        <td>
            @Html.TextBoxFor(modelItem => item.col3)
        </td>
    </tr>
}
</table></center>

Class1.cs
XML
public class Table1
    {
        [Key]
        public int Idt { get; set; }
        public string datetime0 { get; set; }
        public string col1 { get; set; }
        public string col2 { get; set; }
        public string col3 { get; set; }
    }
    public class EditModel
    {
        public string errorcode { get; set; }
        public List<Table1> thistable { get; set; }
    }
    public class testContext : DbContext
    {
        public DbSet<Table1> Data1 { get; set; }
    }
Posted
Updated 19-Jan-15 23:50pm
v2
Comments
Sinisa Hajnal 20-Jan-15 5:57am    
Obviously one of the objects (edit, table1 or even view) is null. Set a breakpoint on the line and check.

Alternatively, check values from col1, 2, 3 etc...from your model.

1 solution

Problem is in your edit.thistable. That is null I think. Debug your code to get clear understanding
 
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