Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将单个对象从SQL Server获取到视图_C#_.net_Linq_Asp.net Mvc 3 - Fatal编程技术网

C# 将单个对象从SQL Server获取到视图

C# 将单个对象从SQL Server获取到视图,c#,.net,linq,asp.net-mvc-3,C#,.net,Linq,Asp.net Mvc 3,我有一个类型为Picture的对象,其中包含一个列表注释,请参见此处的源代码 然后,我想拍摄一张Picture.ID,并从我的数据库中显示具有该唯一ID的图像 因此,我在GalleryController中使用HttpGet做了一个ActionResult注释 然后我有了这个视图,它将显示请求的图像及其列表中的注释 但是我不确定是列表没有初始化,还是我只是做错了,但是我得到了一个错误 “/”应用程序中的服务器错误。 对象引用未设置为 对象的实例。 描述:安 期间发生未处理的异常 当前web的执行

我有一个类型为
Picture
的对象,其中包含一个列表注释,请参见此处的源代码

然后,我想拍摄一张
Picture.ID
,并从我的数据库中显示具有该唯一ID的图像

因此,我在GalleryController中使用HttpGet做了一个ActionResult注释

然后我有了这个视图,它将显示请求的图像及其列表中的注释

但是我不确定是列表没有初始化,还是我只是做错了,但是我得到了一个错误

“/”应用程序中的服务器错误。
对象引用未设置为 对象的实例。
描述:安 期间发生未处理的异常 当前web的执行 要求请查看堆栈跟踪 有关错误的详细信息,请参见 以及它在代码中的起源

异常详细信息:
System.NullReferenceException:对象 引用未设置为的实例 反对


源错误:

让我们检查有问题的操作方法。首先,我看到了这一点,这一点几乎不应该存在:

            catch
            {
            }
所以,如果抛出异常,您只是在吞咽它,永远不会知道它。这将导致返回没有传入模型的视图,因此强类型视图中引用的模型为null。这似乎很有可能发生什么

另外,为什么在
foreach
循环中不断覆盖
ViewBag.Picture
?你真的打算这么做吗


希望这会让你走上正确的道路,你会很快找到根本的问题

因为我不能让它只解析一个对象(而且我知道当它在数据库中搜索主键时,总会有最多一个结果)

所以我的代码现在是

        [HttpGet]
    public ActionResult Comment(string ID)
    {
        int id = Convert.ToInt32(ID);
        if (ID != null)
        {
            try
            {
                var model = from r in _db.Gallery
                            where r.ID == id
                            select r;

                return View(model);
            }
            catch
            {
            }
        }

        return View();
    }

@model IEnumerable
@{
ViewBag.Title=“注释”;
}
@如果(型号!=null)
{
foreach(模型中的var项目)
{
邮寄人
评论
张贴在
@foreach(项目中的var注释。图片注释)
{
@comment.Auther
@注释.文本
@comment.PostedTime
}
}
}

因此,我通过一个我解析的singleitem集合进行了编辑,现在它可以工作了。

你应该尝试在你的帖子中直接发布相关的源代码位-交叉链接到一个单独的站点有点混乱,需要读者大量点击……是的,但是将代码标记为代码看起来仍然非常错误,特别是如果是部分/全部html代码如果您发布代码、XML或数据示例,请在文本编辑器中突出显示这些行,并单击编辑器工具栏上的“代码示例”按钮(
{}
),以很好地格式化和语法突出显示它!
    @model IEnumerable<firstweb4.Models.Picture>

@{
    ViewBag.Title = "Comment";
}

@if (Model != null)
{
    foreach (var item in Model)
    {
    <a href="@item.Path"><img src="@item.Path" alt="@item.Title" width="250px"  /> </a>
    <table>
        <tr>
            <th>
                Posted by
            </th>
            <th>
                Comment
            </th>
            <th>
                Posted on
            </th>
        </tr>    
    @foreach (var comment in item.PictureComments)
    {
        <tr>
            <td>
                @comment.Auther
            </td>
            <td>
                @comment.Text
            </td>
            <td>
                @comment.PostedTime
            </td>
        </tr>
    }
    </table>
    }
}