如何在MVC中编辑动态创建的数据表/ json [英] How do I edit a dynamically created datatable/json in MVC

查看:97
本文介绍了如何在MVC中编辑动态创建的数据表/ json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编辑从随机文件/数据库导入的数据表。我们知道列数或列名。我们需要由用户编辑datatable / Json数据。我们尝试将数据表绑定到MVC中的Webgrid。



我尝试过:

< br $>
控制器:

I need to edit a Datatable which was imported from a random file/database.We do know the number of columns or column name. We need to edit the datatable/Json data by the user. We tried by binding the datatable in to a Webgrid in MVC.

What I have tried:

Controller:

public ActionResult ShowDt()
    {

        var dns = new List<dynamic>();  
        ViewData["msg"] = "Successfully uploaded..";
        var ListData = (DataTable)Session["dtResult"];
        var filter = TempData["dtFilter"];

        ViewBag.MasterChk = ListData;
        dns = ConvertDtToList(ListData);
        ViewBag.Total = dns;
        ViewBag.FilterChk = filter;
        string jsonString = string.Empty;
        jsonString = JsonConvert.SerializeObject(ListData);
        try
        {
            TempData["listdt"] = ListData;
            TempData["dtFilter"] = filter;
            if (ListData == null)
            {
                return RedirectToAction("ExtendFiles");
            }
        }
        catch (Exception ex)
        {
            GF.LogWrite(ex.Message + ex.InnerException, true);
        }
        return View();
    }



查看:




VIEW :

@model System.Data.DataTable
@using System.Data;
@{
ViewBag.Title = "ShowDt";
List<dynamic> obj1 = new List<dynamic>();
  obj1=  ViewBag.Total;
  var grid = new WebGrid(obj1); 
      }
  @grid.GetHtml();



上面的网格应该是可编辑的(列数未知)。有没有办法实现这个目标



我们也试过这个 MVC WebGrid动态列但没有用。



先谢谢!


The above grid should be editable(with number of columns unknown). Is there any way to achieve this

We also tried this MVC WebGrid Dynamic Columns but did not work.

Thanks in Advance!

推荐答案

直接使用DataTable无论如何都不是最佳实践,如果您知道可以创建类并映射字段的所有列,即使您可以使用nuget上可用的newtonsoft,它也会将json数据映射到您的这个。



使用你定义的类,你的代码将变得更容易阅读,你的代码也将变得不那么复杂。



问候,

Santosh
Using DataTable directly is anyways not a best practice, if you are aware of all columns you can create a class and map the fields, even you can use newtonsoft which is available on nuget, it does the mapping of json data to your class.

With your defined class your code will become more easy to read, and your code will also become less complex.

Regards,
Santosh


这篇关于如何在MVC中编辑动态创建的数据表/ json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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