如何摆脱视图中的NullException [英] How do I get rid of the NullException in my View

查看:118
本文介绍了如何摆脱视图中的NullException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MVC5中,我添加了NuGet PagedList.Mvc并使用PagedList放置;以上。



我遇到两件事有问题:

首先,我从线上得到NullException;

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;

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


HomeController.cs

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


Index.cshtml

@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


Class1.cs

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

推荐答案

问题在于你的edit.thistable。我觉得这是空的。调试代码以获得清晰的理解
Problem is in your edit.thistable. That is null I think. Debug your code to get clear understanding


这篇关于如何摆脱视图中的NullException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆