1

我必须毫不犹豫地谈论我的问题。我从我的项目中拍摄照片,因为它对 Gide 我更有帮助。我有一个 Telerik MVC 网格,它没有任何项目,当用户单击第 1 个弹出窗口显示和用户时单击代码并在 3 状态下用户单击添加新按钮和与所选 id 数据相关的一行添加到网格我的问题是我通过 Json 处理此问题但我在 firebug 中收到 500 错误并且我在我的网格中看不到任何结果任何建议都将受到欢迎感谢提前在此处输入图像描述

$("#btnadd").button().click(function () {
        $.ajax({
            dataType: "json",
            url: "../Shared/GetItems",
            data: $("#SparePartCode").val(),
            success: function (json) {
                var grid = $('#InvoiceItemGrid').data('tGrid');
                alert("jsonresualt");
                grid.dataBind(json);
            }
        });
    });
    private IList<PartBooklet> GetPartBooklet(int sparepart)
        {
            return _PartBookletService.GetList().Where(m => m.SparePartCode == sparepart).ToList();
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult GetItems(int sparepart)
        {
            var PartbookletList = this.GetPartBooklet((sparepart));
            var partbookletData = PartbookletList.Select(m => new SelectListItem()
            {
                Text = m.Title,
                Value = m.Id.ToString(),
            });

            return Json(partbookletData, JsonRequestBehavior.AllowGet);
        }
4

1 回答 1

0

这个对我有用。请找到下面的代码,了解如何通过 Json 在 mvc Telerik 网格中动态添加行。

MVC 剃刀 html

<div  style="height:100%; overflow:scroll;" >
            @(Html.Telerik().Grid<VehicleSurveyAttachmentModel>()
                .Name("GridFilelist")
                .DataKeys(dk => dk.Add(gfl => gfl.RecordNo))
                .Columns(column =>
                    {
                        column.Bound(f => f.RecordNo).Hidden(true);
                        column.Template(@<text><img alt="attachedImage" class="surveyImg" src="@Url.Content("../Images/gui_logo.gif")" /> </text>);
                        column.Command(cmd => { cmd.Delete().Text("Remove").ButtonType(GridButtonType.Image); });
                    }                    
                    )
                    .DataBinding(dataBinding => dataBinding.Ajax()
                                .Delete("_AjaxDeleteImage", "VehicleSurvey"))                                                    
                    .Footer(false)
                    .HtmlAttributes(new { @style = "width:100%" })
                    .Scrollable()
            )
            </div>

jQuery

  $(function () {
            $("#fattach").change(function () {
                alert($("#fattach").val().substring($("#fattach").val().lastIndexOf('\\') + 1));
                var attachfile = $('#fattach').val();
                attachList(attachfile);
            });
            $("#fcapture").change(function () {
                alert($("#fcapture").val());
            });
        });

        function attachList(filename)
        {
            //debugger;
            $.ajax({
                url: '/VehicleSurvey/UpdateFileList',
                type: 'POST',
                data: { fname: filename },
                dataType: "json",
                success: function (result) {
                    var grid = $("#GridFilelist").data("tGrid");
                    grid.dataBind(result.result);
                 //   $('#GridFilelist').data('t-grid').ajaxRequest();
                },
                error: function () {
                    alert("error");
                }
            });
        }

MVC3

 // Add attachment to GridList dynamically.
        [HttpPost]
        public JsonResult UpdateFileList(string fname)
        {
            VehicleSurveyAttachmentModel vsattach = new VehicleSurveyAttachmentModel();
            TryUpdateModel(vsattach);
            vsattach.Status = GridProcess.Insert;
            List<VehicleSurveyAttachmentModel> listImgFiles = new List<VehicleSurveyAttachmentModel>();
            listImgFiles = (List<VehicleSurveyAttachmentModel>)Session["filelist"];
            if (listImgFiles != null && listImgFiles.Count > 0)
            {
                var indexmatch = listImgFiles.Select(a => a.index).Max();
                vsattach.index = indexmatch + 1;
                vsattach.FileName = fname;
            }
            else { vsattach.index = 1; vsattach.FileName = fname; }
            listImgFiles.Add(vsattach);
            Session["filelist"] = listImgFiles;
            return Json(new {result = listImgFiles.ToList() } , JsonRequestBehavior.AllowGet );
        }
于 2015-09-30T07:12:50.407 回答