Url.content行没有进入我的行动

时间:2015-09-22 11:35:13

标签: jquery json ajax asp.net-mvc

  

" url":" @ string.Format(" {0}:// {1} {2}",Request.Url.Scheme,Request。 Url.Authority,Url.Content("〜"))/ AjaxGetJsonData",

当编译器转到视图中的上述代码行时,它应该转移到AjaxGetJsonData动作。但是它终止了程序,数据表显示了处理结果

在视图中

<script src="~/Scripts/jquery-1.7.2.js"></script>
<script src="~/Scripts/jquery.dataTables.js" type="text/javascript"></script>
<link href="~/Content/dataTables/jquery.dataTables.css" rel="stylesheet" />
<script src="~/Scripts/metro.js"></script>
<script>
$(document).ready(function () {
    $('#example').dataTable({
        "processing": true, // control the processing indicator.
        "serverSide": true, // recommended to use serverSide when data is more than 10000 rows for performance reasons
        "info": true,   // control table information display field
        "stateSave": true,  //restore table state on page reload,
        "lengthMenu": [[10, 20, 50, -1], [10, 20, 50, "All"]],    // use the first inner array as the page length values and the second inner array as the displayed options
        "ajax": {
            "url": "@string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Authority, Url.Content("~"))/AjaxGetJsonData",
            "type": "GET"
        },
        "columns": [
            { "data": "ProductName", "orderable": true },
            { "data": "Price", "orderable": false },
            { "data": "PackSize", "orderable": true },
            { "data": "FormName", "orderable": true }
        ],
        "order": [[0, "asc"]]
    });
});
</script>

<table id="example" class="display" cellspacing="0" width="100%">
    <thead>
        <tr style="text-align:left;">
            <th>Name</th>
            <th>Age</th>
            <th>DoB</th>
        </tr>
    </thead>

    <tfoot>
        <tr style="text-align:left;">
            <th>Name</th>
            <th>Age</th>
            <th>DoB</th>
        </tr>
    </tfoot>
</table>

在控制器AllProdDetailsController

public class AllProdDetailsController : Controller
{

    private const int TOTAL_ROWS = 4;
    //private static readonly List<DataItem> _data;
    private static readonly List<DataItem> _data = CreateData();
    public static List<BOL.tbl_Product> _datas()
    {
        BLL.IRepository<BOL.tbl_Product> rep = new BLL.IRepository<BOL.tbl_Product>();
        var dtsource = rep.GetAll().ToList();
        return dtsource;
    }

    public class DataItem
    {
        public string ProductName { get; set; }
        public string PackSize { get; set; }
        public string Price { get; set; }
        public string FormName { get; set; }

    }
    public class DataTableData
    {
        public int draw { get; set; }
        public int recordsTotal { get; set; }
        public int recordsFiltered { get; set; }
        public List<DataItem> data { get; set; }
    }

    private static List<DataItem> CreateData()
    {
        List<DataItem> list = new List<DataItem>();

        List<BOL.tbl_Product> prod = _datas().ToList();
        for (int i = 0; i < prod.Count; i++)
        {
            DataItem item = new DataItem();
            item.ProductName = prod[i].ProductName;
            item.Price = prod[i].Price;
            item.PackSize = prod[i].PackSize;
            item.FormName = prod[i].tbl_Medicine_form.FormName;
            list.Add(item);
        }
        return list;

    }
    private int SortString(string s1, string s2, string sortDirection)
    {
        return sortDirection == "asc" ? s1.CompareTo(s2) : s2.CompareTo(s1);
    }

    private int SortInteger(string s1, string s2, string sortDirection)
    {
        int i1 = int.Parse(s1);
        int i2 = int.Parse(s2);
        return sortDirection == "asc" ? i1.CompareTo(i2) : i2.CompareTo(i1);
    }

    private int SortDateTime(string s1, string s2, string sortDirection)
    {
        DateTime d1 = DateTime.Parse(s1);
        DateTime d2 = DateTime.Parse(s2);
        return sortDirection == "asc" ? d1.CompareTo(d2) : d2.CompareTo(d1);
    }

    private List<DataItem> FilterData(ref int recordFiltered, int start, int length, string search, int sortColumn, string sortDirection)
    {
        List<DataItem> list = new List<DataItem>();
        if (search == null)
        {
            list = _data;
        }
        else
        {
            // simulate search
            foreach (DataItem dataItem in _data)
            {
                if (dataItem.ProductName.ToUpper().Contains(search.ToUpper()) ||
                    dataItem.Price.ToString().Contains(search.ToUpper()) ||
                    dataItem.PackSize.ToString().Contains(search.ToUpper()) ||
                dataItem.FormName.ToString().Contains(search.ToUpper()))
                {
                    list.Add(dataItem);
                }
            }
        }

        // simulate sort
        if (sortColumn == 0)
        {// sort Name
            list.Sort((x, y) => SortString(x.ProductName, y.ProductName, sortDirection));
        }
        else if (sortColumn == 1)
        {// sort Age
            list.Sort((x, y) => SortInteger(x.Price, y.Price, sortDirection));
        }
        else if (sortColumn == 2)
        {   // sort DoB
            list.Sort((x, y) => SortString(x.PackSize, y.PackSize, sortDirection));
        }
        else if (sortColumn == 3)
        {   // sort DoB
            list.Sort((x, y) => SortString(x.FormName, y.FormName, sortDirection));
        }

        recordFiltered = list.Count;

        // get just one page of data
        list = list.GetRange(start, Math.Min(length, list.Count - start));

        return list;
    }


  public ActionResult AjaxGetJsonData(int draw, int start, int length)
  {
        string search = Request.QueryString["search[value]"];
        int sortColumn = -1;
        string sortDirection = "asc";
        if (length == -1)
        {
            length = TOTAL_ROWS;
        }

        // note: we only sort one column at a time
        if (Request.QueryString["order[0][column]"] != null)
        {
            sortColumn = int.Parse(Request.QueryString["order[0][column]"]);
        }
        if (Request.QueryString["order[0][dir]"] != null)
        {
            sortDirection = Request.QueryString["order[0][dir]"];
        }

        DataTableData dataTableData = new DataTableData();
        dataTableData.draw = draw;
        dataTableData.recordsTotal = TOTAL_ROWS;
        int recordsFiltered = 0;
        dataTableData.data = FilterData(ref recordsFiltered, start, length, search, sortColumn, sortDirection);
        dataTableData.recordsFiltered = recordsFiltered;

        return Json(dataTableData, JsonRequestBehavior.AllowGet);
    }

    public ActionResult Index()
    {
        //_datas();
        return View();
    }

1 个答案:

答案 0 :(得分:1)

您应该使用UrlHelper.Action Method (String, String)

"url": "@Url.Action("AjaxGetJsonData", "AllProdDetails")"