【问题标题】:jqGrid show image in row from databasejqGrid 从数据库中按行显示图像
【发布时间】:2011-01-27 08:12:01
【问题描述】:

我试图在 jqGrid 中显示来自记录的图像,但它不起作用。

我的 jqGrid 中的每条记录都有一个 id。为了从我的数据库表中获取图像,我编写了一个 ActionResult,它将存储在数据库表中的文件(图像)返回到 id。

因为每条记录都有一个唯一的 id,所以我在我的页面中有一个隐藏字段,jq 应该存储格式化到格式化程序的实际记录的实际 id。

当我用firebug查看代码时,似乎隐藏字段的方式不起作用。

也许你有一个想法?

这是我的代码:

<input type="hidden" name="cellvalue" value="" />
<script type="text/javascript">
$(function () {
    $("#PartialIndexGrid").jqGrid({
        url: '/@ViewContext.RouteData.Values["Controller"].ToString()/IndexGridData',
        datatype: 'json',
        mtype: 'POST',
        colNames: ['Details', 'Bearbeiten','Bild', 'Titel', 'Bearbeitungsort', 'Status'],
        colModel: [
              { name: 'Details', index: "Details", edittype: 'select', align: "center", width: 45, formatter: 'showlink', formatoptions: { baseLinkUrl: '/Shared/Details/', addParam: ''} },
              { name: 'Bearbeiten', index: "Bearbeiten", edittype: 'select', align: "center", width: 80, formatter: 'showlink', formatoptions: { baseLinkUrl: '/Shared/Edit/', addParam: ''} },
              { name: 'Bild', index: 'Bild', edittype: 'image', formatter: imageFormatter },
              { name: 'Titel', index: 'Titel'},
              { name: 'Bearbeitungsort', index: 'Bearbeitungsort' },
              { name: 'AuftragStatus', index: 'AuftragStatus'}
            ],
        pager: $("#PartialIndexGridpager"),
        rowNum: 10,
        rowList: [5, 10, 20, 30],
        sortname: 'Titel',
        sortorder: "asc",
        viewrecords: true,
        width: 942, 
        caption: ''
    })
});
function imageFormatter(cellvalue, options, rowObject) {
        $("cellvalue").val(cellvalue);
        return '<img src="@Url.Action("AuftragDBImage", "Shared", new { id = Request.Form["cellvalue"]})" />';
}; 

public ActionResult AuftragDBImage(Guid id)
    {
        try
        {
            var auftrag = _db.Auftrag.Where(x => x.Auftrag_GUID == id).Select(x => x).Single();
            return File(auftrag.Bild, "image/jpeg");
        }
        catch (Exception)
        {

            return File(pfaddummybild, "image/jpeg");
        }
    }

问候, 浮动

【问题讨论】:

  • 我在jqGrid中没有找到任何隐藏字段,但是您写道“似乎隐藏字段的方式不起作用”。此外,自定义格式化程序imageFormatter 看起来很奇怪。为了让其他人能够验证您的代码,您应该说明 Build 的哪些值具有来自服务器的 JSON 响应以及自定义格式化程序应该产生什么结果。

标签: c# asp.net jquery asp.net-mvc jqgrid


【解决方案1】:

您是否注册了格式化程序?我认为您需要在加载网格之前执行此操作。这是一个例子:

$.fn.fmatter.imageFormatter = function(cellvalue, options, rowObject) { return ""; }; 脚本>

请注意,您在格式化程序中没有 Request.Form 对象(请记住您在客户端上),因此请使用常规 url。

【讨论】:

  • 你对 Request.Form 对象是正确的。这就是问题所在。现在它进入我的控制器操作。但它无需格式化程序注册即可工作。
  • 当然也不需要使用隐藏字段,因为单元格值是格式化程序中的变量。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-03
  • 2016-03-14
  • 1970-01-01
  • 2015-02-12
  • 2021-09-07
  • 1970-01-01
  • 2018-07-17
相关资源
最近更新 更多