如何在Json的mvc Telerik网格中动态添加行?

时间:2013-09-18 07:03:35

标签: json jquery asp.net-mvc-4 telerik-grid telerik-mvc

没有任何失去的时间我不得不说我的问题。我从我的项目中获取图片,因为它对Gide我更有帮助。我有一个Telerik MVC网格它没有任何项目,当用户点击1号弹出窗口时显示和用户点击代码和3状态用户点击添加新按钮和一行与选定的id数据相关添加到网格我的问题是我处理这个问题由Json但我得到500错误在firebug我看不到任何结果在我的网格中,任何建议都会受到欢迎,感谢提前enter image description here

$("#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);
        }

1 个答案:

答案 0 :(得分:0)

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

MVC Razor 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 );
        }