Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.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
Javascript 林克上市<;AJAX>;在数据表中使用_Javascript_C#_Asp.net_Ajax_Linq - Fatal编程技术网

Javascript 林克上市<;AJAX>;在数据表中使用

Javascript 林克上市<;AJAX>;在数据表中使用,javascript,c#,asp.net,ajax,linq,Javascript,C#,Asp.net,Ajax,Linq,我试图执行一个LINQ查询来填充一个AJAX列表,然后返回到视图中的数据表中使用 我以前没有尝试实现AJAX,只是将其放在模型中,并在循环中呈现表中的每一行,这一点就可以实现了。然而,这是令人难以置信的资源要求,并采取了太长的时间来呈现在我的更大的查询页面 所以我尝试实现AJAX,不管LINQ返回空值。我知道我可以正确地获取数据,因为我已经实现了一个测试用例,它返回了一个简单的AJAX小样本,但是由于某种原因,它并没有填充我的数据表 因此,简而言之,我需要:修复我的LINQ列表并使用它填充数据表

我试图执行一个LINQ查询来填充一个AJAX列表,然后返回到视图中的数据表中使用

我以前没有尝试实现AJAX,只是将其放在模型中,并在循环中呈现表中的每一行,这一点就可以实现了。然而,这是令人难以置信的资源要求,并采取了太长的时间来呈现在我的更大的查询页面

所以我尝试实现AJAX,不管LINQ返回空值。我知道我可以正确地获取数据,因为我已经实现了一个测试用例,它返回了一个简单的AJAX小样本,但是由于某种原因,它并没有填充我的数据表

因此,简而言之,我需要:修复我的LINQ列表并使用它填充数据表

这是我的控制器:

public class stockAJAX
{
    public int StockId { get; set; }
    public string ProductGroup { get; set; }
    public string GroupType { get; set; }
    public string ItemType { get; set; }
    public string Model { get; set; }
    public string SerialNo { get; set; }
    public string NR { get; set; }
    public string Status { get; set; }
    public string Description { get; set; }
    public string DateArrived { get; set; }
    public int? CurrentLocation { get; set; }
    public string TerminalId { get; set; }
}

public class TableController : Controller
{
    List<stockAJAX> stock = new List<stockAJAX>();
    stockAJAX ajaxTemp = new stockAJAX();
    static string csv;


[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
public JsonResult getAJAX(System.Web.Mvc.FormCollection collection)
{
    //Recieve Data from the Select Company DropDownList
    string selectedList = collection["list"];
    //Recieve Data from the Select GroupType DropDownList
    string selectedGroupType = collection["grouptype"];
    //Recieve Data from the Show All Stock checkbox
    string selectedAmount = collection["amount"];

    //A list of type <stock> has its value recieved from the function which computes which query to use and then executes it.
    stock = returnList(selectedGroupType, selectedList, selectedAmount);

    if (stock == null)
    {
        System.Windows.Forms.MessageBox.Show("NULL AJAX");

        stockAJAX stock1 = new stockAJAX();
        stock1.StockId = 0;
        stock1.ProductGroup = " ";
        stock1.GroupType = " ";
        stock1.ItemType = " ";
        stock1.Model = " ";
        stock1.SerialNo = " ";
        stock1.NR = " ";
        stock1.Status = " ";
        stock1.Description = " ";
        stock1.DateArrived = " ";
        stock1.CurrentLocation = 0;
        stock1.TerminalId = " ";
        return Json(stock1, JsonRequestBehavior.AllowGet);
    }


    return Json(stock, JsonRequestBehavior.AllowGet);
}

//Function that grabs the data for the DataTable, and the two DropDownLists, the two DropDownLists are return through ViewData[] and the DataTable data is returned via Model
//Takes three strings which are taken from Form Data
public List<stockAJAX> returnList(string selectedGroupType, string selectedList, string selectedAmount)
{
    //List of type <stock> this will contain all of the data to be put into the DataTable



    //Namespace utilising the TableEntities connection to the Entity Framework
    using (TableEntities context = new TableEntities())
    {
        //Lists which will contain data for the DropDownLists
        IList<SelectListItem> ddl = new List<SelectListItem>();
        IList<SelectListItem> ddl2 = new List<SelectListItem>();

        //Temp list of strings, used to grab data from the Entity to be placed into the dropdownlists
        List<string> stocktemp = null;

        //Query for the ProductGroup DropDownList
        stocktemp = (from c in context.stocks
                     select c.ProductGroup
                    ).Distinct().ToList();
        foreach (string item in stocktemp)  //Places data received in the SelectList as items
        {
            if (item == selectedList && item != null)       //Tests to see if the Item was selected in the last view, if so is set as default
                ddl.Add(new SelectListItem() { Text = item, Selected = true });
            else if (item != null)
                ddl.Add(new SelectListItem() { Text = item });
        }
        ViewData["list"] = ddl;         //PLaces the SelectList in the ViewData[]


        //Query for the GroupType DropDownList
        stocktemp = (from c in context.stocks
                     select c.GroupType
                    ).Distinct().ToList();
        foreach (string item in stocktemp)//Tests to see if the Item was selected in the last view, if so is set as default
        {
            if (item == selectedGroupType && item != null)
                ddl2.Add(new SelectListItem() { Text = item, Selected = true });
            else if (item != null)
                ddl2.Add(new SelectListItem() { Text = item });
        }
        ViewData["grouptype"] = ddl2;   //PLaces the SelectList in the ViewData[]

        //If the user selected to show all data, omits the Take(1000) function
        if (selectedAmount == "true")
        {
            //If the Value is null for both, then it will return no data.
            //This case is if the User selected the default non-value option for each
            if (selectedList == null && selectedGroupType == null)
            {
                stock = (from c in context.stocks
                         select new stockAJAX
                         {
                             StockId = c.StockId,
                             ProductGroup = c.ProductGroup,
                             GroupType = c.GroupType,
                             ItemType = c.ItemType,
                             Model = c.Model,
                             SerialNo = c.SerialNo,
                             NR = c.NR,
                             Status = c.Status.ToString(),
                             Description = c.Description,
                             DateArrived = c.DateArrived.ToString(),
                             CurrentLocation = c.CurrentLocation,
                             TerminalId = c.TerminalId,
                         }
                        ).ToList();
            }
            else if (selectedGroupType == "grouptype=Select+GroupType" || selectedGroupType == null || selectedGroupType == "")     //If SelecteGroupType is NULL, then no value was selected for Select GroupType
            {
                if (selectedList == null || selectedList == "")     //if selectedList is also NULL return nothing
                {
                    stock = (from c in context.stocks
                             select new stockAJAX
                             {
                                 StockId = c.StockId,
                                 ProductGroup = c.ProductGroup,
                                 GroupType = c.GroupType,
                                 ItemType = c.ItemType,
                                 Model = c.Model,
                                 SerialNo = c.SerialNo,
                                 NR = c.NR,
                                 Status = c.Status.ToString(),
                                 Description = c.Description,
                                 DateArrived = c.DateArrived.ToString(),
                                 CurrentLocation = c.CurrentLocation,
                                 TerminalId = c.TerminalId,
                             }
                        ).ToList();
                }
                else                    //Else, if selectedList is not null, return data from query using selectedList as the testing for ProductGroup
                {
                    stock = (from c in context.stocks
                             where c.ProductGroup == selectedList
                             select new stockAJAX
                             {
                                 StockId = c.StockId,
                                 ProductGroup = c.ProductGroup,
                                 GroupType = c.GroupType,
                                 ItemType = c.ItemType,
                                 Model = c.Model,
                                 SerialNo = c.SerialNo,
                                 NR = c.NR,
                                 Status = c.Status.ToString(),
                                 Description = c.Description,
                                 DateArrived = c.DateArrived.ToString(),
                                 CurrentLocation = c.CurrentLocation,
                                 TerminalId = c.TerminalId,
                             }
                        ).ToList();
                }
            }
            else if (selectedList == "list=Select+Company" || selectedList == null || selectedList == "")       //if select list is null, then no value was selected for Select Company
            {
                stock = (from c in context.stocks
                         where c.GroupType == selectedGroupType
                         select new stockAJAX
                         {
                             StockId = c.StockId,
                             ProductGroup = c.ProductGroup,
                             GroupType = c.GroupType,
                             ItemType = c.ItemType,
                             Model = c.Model,
                             SerialNo = c.SerialNo,
                             NR = c.NR,
                             Status = c.Status.ToString(),
                             Description = c.Description,
                             DateArrived = c.DateArrived.ToString(),
                             CurrentLocation = c.CurrentLocation,
                             TerminalId = c.TerminalId,
                         }
                        ).ToList();
            }
            else                        //else if grouptype is not null, retrun data from query using groupType as the testing for GroupType
            {
                stock = (from c in context.stocks
                         where c.ProductGroup == selectedList
                         where c.GroupType == selectedGroupType
                         select new stockAJAX
                         {
                             StockId = c.StockId,
                             ProductGroup = c.ProductGroup,
                             GroupType = c.GroupType,
                             ItemType = c.ItemType,
                             Model = c.Model,
                             SerialNo = c.SerialNo,
                             NR = c.NR,
                             Status = c.Status.ToString(),
                             Description = c.Description,
                             DateArrived = c.DateArrived.ToString(),
                             CurrentLocation = c.CurrentLocation,
                             TerminalId = c.TerminalId,
                         }
                        ).ToList();
            }
        }
        else        //If Show All was not checked, then select top 1000 ordered by descending date
        {
            //If the Value is null for both, then it will return no data.
            //This case is if the User selected the default non-value option for each
            if (selectedList == null && selectedGroupType == null)
            {
                stock = null;
            }
            else if (selectedGroupType == "grouptype=Select+GroupType" || selectedGroupType == null || selectedGroupType == "")     //If SelecteGRoupType is NULL, then no value was selected for Select GroupType
            {
                if (selectedList == null || selectedList == "")     //if selectedList is also NULL return nothing
                {
                    stock = null;
                }
                else                //Else, if selectedList is not null, return data from query using selectedList as the testing for ProductGroup
                {
                    stock = (from c in context.stocks
                             where c.ProductGroup == selectedList
                             orderby c.DateArrived descending
                             select new stockAJAX
                         {
                             StockId = c.StockId,
                             ProductGroup = c.ProductGroup,
                             GroupType = c.GroupType,
                             ItemType = c.ItemType,
                             Model = c.Model,
                             SerialNo = c.SerialNo,
                             NR = c.NR,
                             Status = c.Status.ToString(),
                             Description = c.Description,
                             DateArrived = c.DateArrived.ToString(),
                             CurrentLocation = c.CurrentLocation,
                             TerminalId = c.TerminalId,
                         }
                        ).Take(1000).ToList();
                }
            }
            else if (selectedList == "list=Select+Company" || selectedList == null || selectedList == "")   //if select list is null, then no value was selected for Select Company
            {
                stock = (from c in context.stocks
                         where c.GroupType == selectedGroupType
                         orderby c.DateArrived descending
                         select new stockAJAX
                         {
                             StockId = c.StockId,
                             ProductGroup = c.ProductGroup,
                             GroupType = c.GroupType,
                             ItemType = c.ItemType,
                             Model = c.Model,
                             SerialNo = c.SerialNo,
                             NR = c.NR,
                             Status = c.Status.ToString(),
                             Description = c.Description,
                             DateArrived = c.DateArrived.ToString(),
                             CurrentLocation = c.CurrentLocation,
                             TerminalId = c.TerminalId,
                         }
                        ).Take(1000).ToList();
            }
            else                //else if grouptype is not null, retrun data from query using groupType as the testing for GroupType
            {
                stock = (from c in context.stocks
                         where c.ProductGroup == selectedList
                         where c.GroupType == selectedGroupType
                         orderby c.DateArrived descending
                         select new stockAJAX
                         {
                             StockId = c.StockId,
                             ProductGroup = c.ProductGroup,
                             GroupType = c.GroupType,
                             ItemType = c.ItemType,
                             Model = c.Model,
                             SerialNo = c.SerialNo,
                             NR = c.NR,
                             Status = c.Status.ToString(),
                             Description = c.Description,
                             DateArrived = c.DateArrived.ToString(),
                             CurrentLocation = c.CurrentLocation,
                             TerminalId = c.TerminalId,
                         }
                        ).Take(1000).ToList();
            }
        }
    }


    //return the value to be used by the DataTable
    return stock;
}

//Accept GET and POST
[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
public ActionResult Index(System.Web.Mvc.FormCollection collection)
{
    //DateTime lastMonth = DateTime.Today.AddMonths(-6);

    //Recieve Data from the Select Company DropDownList
    string selectedList = collection["list"];
    //Recieve Data from the Select GroupType DropDownList
    string selectedGroupType = collection["grouptype"];
    //Recieve Data from the Show All Stock checkbox
    string selectedAmount = collection["amount"];

    //A list of type <stock> has its value recieved from the function which computes which query to use and then executes it.
    returnList(selectedGroupType, selectedList, selectedAmount);

    //Returns the view
    return View();
公共类stockAJAX
{
public int StockId{get;set;}
公共字符串ProductGroup{get;set;}
公共字符串GroupType{get;set;}
公共字符串ItemType{get;set;}
公共字符串模型{get;set;}
公共字符串SerialNo{get;set;}
公共字符串NR{get;set;}
公共字符串状态{get;set;}
公共字符串说明{get;set;}
公共字符串DateArrived{get;set;}
公共int?当前位置{get;set;}
公共字符串TerminalId{get;set;}
}
公共类TableController:控制器
{
列表库存=新列表();
stockAJAX ajaxTemp=newstockajax();
静态字符串csv;
[接受动词(HttpVerbs.Get | HttpVerbs.Post)]
公共JsonResult getAJAX(System.Web.Mvc.FormCollection)
{
//从选择公司下拉列表接收数据
string selectedList=collection[“list”];
//从选择GroupType下拉列表接收数据
字符串selectedGroupType=collection[“grouptype”];
//从“显示所有库存”复选框接收数据
字符串selectedAmount=collection[“amount”];
//类型列表的值从函数接收,该函数计算要使用的查询,然后执行该查询。
stock=返回列表(selectedGroupType、selectedList、selectedAmount);
如果(股票==null)
{
System.Windows.Forms.MessageBox.Show(“空AJAX”);
stockAJAX stock1=新的stockAJAX();
stock1.StockId=0;
stock1.ProductGroup=“”;
stock1.GroupType=“”;
stock1.ItemType=“”;
stock1.Model=“”;
stock1.SerialNo=“”;
股票1.NR=“”;
股票1.状态=”;
stock1.Description=“”;
stock1.DateArrived=“”;
stock1.CurrentLocation=0;
stock1.TerminalId=“”;
返回Json(stock1,JsonRequestBehavior.AllowGet);
}
返回Json(stock,JsonRequestBehavior.AllowGet);
}
//函数获取DataTable和两个DropDownList的数据,两个DropDownList通过ViewData[]返回,DataTable数据通过Model返回
//从表单数据中获取三个字符串
公共列表返回列表(字符串selectedGroupType、字符串selectedList、字符串selectedAmount)
{
//此类型的列表将包含要放入DataTable的所有数据
//使用TableEntities连接到实体框架的命名空间
使用(TableEntities上下文=新TableEntities())
{
//包含下拉列表数据的列表
IList ddl=新列表();
IList ddl2=新列表();
//临时字符串列表,用于从要放入DropDownList的实体中获取数据
List stocktemp=null;
//查询ProductGroup DropDownList
stocktemp=(来自context.stocks中的c)
选择c.ProductGroup
).Distinct().ToList();
foreach(stocktemp中的字符串项)//将在SelectList中接收的数据作为项放置
{
if(item==selectedList&&item!=null)//测试项目是否在上一个视图中被选中,如果是,则设置为默认值
Add(new SelectListItem(){Text=item,Selected=true});
else if(项!=null)
Add(新的SelectListItem(){Text=item});
}
ViewData[“list”]=ddl;//将SelectList放置在ViewData[]
//查询GroupType DropDownList
stocktemp=(来自context.stocks中的c)
选择c.GroupType
).Distinct().ToList();
foreach(stocktemp中的string item)//测试该项是否在最后一个视图中被选中,如果是,则设置为默认值
{
if(item==selectedGroupType&&item!=null)
ddl2.Add(新的SelectListItem(){Text=item,Selected=true});
else if(项!=null)
ddl2.Add(新建SelectListItem(){Text=item});
}
ViewData[“grouptype”]=ddl2;//将选择列表放置在ViewData[]
//如果用户选择显示所有数据,则忽略Take(1000)功能
如果(selectedAmount==“true”)
{
//如果两者的值都为null,则它将不返回任何数据。
//这种情况下,如果用户为每个选项选择了默认的非值选项
if(selectedList==null&&selectedGroupType==null)
{
stock=(来自context.stocks中的c)
选择新stockAJAX
{
StockId=c.StockId,
ProductGroup=c.ProductGroup,
GroupType=c.GroupType,
ItemType=c.ItemType,
模型=c.模型,
SerialNo=c.SerialNo,
NR=c.NR,
Status=c.Status.ToString(),
描述=c.描述,
DateArrized=c.DateArrized.ToString(),
CurrentLocation=c.CurrentLocation,
var Json = [
    { StockId: 0, 
    ProductGroup: 0, 
    GroupType: 0,
    ItemType: 0 ,
    Model: 0 ,
    SerialNo: 0,
    NR: 0 ,
    Status: 0,
    Description: 0,
    DateArrived: 0 ,
    CurrentLocation: 0 ,
    TerminalId: 0,
    },
    ];

    $("#myTable").DataTable({
        "JQueryUI": true,
        "stateSave": true,
        "ajax": $.ajax({
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            type: 'GET',
            url: '/Table/getAJAX',
            data: Json,
            success: function(Json)
            {
                if (Json.length == 0 || Json == null)
                    alert(post);
                var _len = Json.length, post, i;

                for (i = 0; i < _len; i++) {
                    post = Json[i];
                    alert(post);
                }
            },
            failure: function () { alert("unavailable"); },
        })
    });
    $('#loading').hide();
    $('#myTable').show();
<div class="filter">
<form class="my-form" method="post" action="~/Table/Index">
    <br />
    @Html.DropDownList("list", "Select Company")
    @Html.DropDownList("grouptype", "Select GroupType")
    <br />
    <button input type="submit"> Submit </button>
    <input type="checkbox" name="amount" id="amount" value="true"><p2>Show All Company Stock</p2>
    <br /><br />
</form>
</div>
<table class="table-fill" id="myTable">
        <thead>
            <tr>
                <th>
                    <p1>Stock Id</p1>
                </th>
                <th>
                    <p1>Product Group</p1>
                </th>
                <th>
                    <p1>Group Type</p1>
                </th>
                <th>
                    <p1>Item Type</p1>
                </th>
                <th>
                    <p1>Model</p1>
                </th>
                <th>
                    <p1>Serial No</p1>
                </th>
                <th>
                    <p1>NR</p1>
                </th>
                <th>
                    <p1>Status</p1>
                </th>
                <th>
                    <p1>Description</p1>
                </th>
                <th>
                    <p1>Date Arrived</p1>
                </th>
                <th>
                    <p1>Current Location</p1>
                </th>
                <th>
                    <p1>Terminal ID</p1>
                </th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <th>Id</th>
                <th>Product</th>
                <th>Group</th>
                <th>Item</th>
                <th>Model</th>
                <th>Serial</th>
                <th>NR</th>
                <th>Status</th>
                <th>Descr</th>
                <th>Date</th>
                <th>Location</th>
                <th>T-ID</th>
            </tr>
        </tfoot>


    </table>
public List<stockAJAX> returnList(string selectedGroupType, string selectedList, string selectedAmount)
{
    using (TableEntities context = new TableEntities())
    {
        // Do base query
        IQueryable<stock> stocks = context.stocks.AsQueryable();

        // Do filtering here
        if (selectedList == null && selectedGroupType == null)
        {
          // return all the records
        }
        else if (selectedGroupType == "grouptype=Select+GroupType" || selectedGroupType == null || selectedGroupType == "")     //If SelecteGroupType is NULL, then no value was selected for Select GroupType
        {
            if (selectedList == null || selectedList == "")     //if selectedList is also NULL return nothing
            {
              // return all the records
            }
            else                    //Else, if selectedList is not null, return data from query using selectedList as the testing for ProductGroup
            {
                stocks = stocks.Where(c=>c.ProductGroup == selectedList);
            }
        }
        else if (selectedList == "list=Select+Company" || selectedList == null || selectedList == "")       //if select list is null, then no value was selected for Select Company
        {
            stocks = stocks.Where(c=>c.GroupType == selectedGroupType);
        }
        else                        //else if grouptype is not null, retrun data from query using groupType as the testing for GroupType
        {
            stocks = stocks
              .Where(c=>c.ProductGroup == selectedList)
              .Where(c=>c.GroupType == selectedGroupType);
        }

      // Apply limit
      if (selectedAmount != "true") 
      {
        stocks = stocks
          .OrderByDescending(c=>c.DateArrived)
          .Take(1000);
      }

      // Do projection to DTO
      var result = stocks.Select(c=>new stockAJAX
      {
        StockId = c.StockId,
        ProductGroup = c.ProductGroup,
        GroupType = c.GroupType,
        ItemType = c.ItemType,
        Model = c.Model,
        SerialNo = c.SerialNo,
        NR = c.NR,
        Status = c.Status.ToString(),
        Description = c.Description,
        DateArrived = c.DateArrived.ToString(),
        CurrentLocation = c.CurrentLocation,
        TerminalId = c.TerminalId,
      }).ToList();
    }

    //return the value to be used by the DataTable
    return result;
}
[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
        public JsonResult getAJAX()
        {
            using (TableEntities context = new TableEntities())
            {
                stock = (from c in context.stocks
                         select new stockAJAX
                         {
                             StockId = c.StockId,
                             ProductGroup = c.ProductGroup,
                             GroupType = c.GroupType,
                             ItemType = c.ItemType,
                             Model = c.Model,
                             SerialNo = c.SerialNo,
                             NR = c.NR,
                             Status = c.Status.ToString(),
                             Description = c.Description,
                             DateArrived = c.DateArrived.ToString(),
                             CurrentLocation = c.CurrentLocation,
                             TerminalId = c.TerminalId,
                         }
                                    ).Take(1000).ToList();
            }

            return Json(stock, JsonRequestBehavior.AllowGet);
        }