1

我正在尝试根据下拉选择的值过滤我的结果。所有的过滤和一切都在工作,我只是在努力让我的观点更新结果。我省略了一些括号和其他不相关的代码这是我所拥有的:

public ViewResult Index()
{    
     -- this effectively returns all Invoices no matter what date --
     var data = new UserBAL().GetInvoice(date);
     return View(data);                 
}

我的 Jquery 和 Ajax 是:

 $(document).ready(function () {
     $("[name='DDLItems']").change(function () {
         var selection = $("[name='DDLItems']").val();
         var dataToSend = {
             //variable to hold selection?
             idDate: selection
         };

         $.ajax({
             type: "POST",
             url: "Invoice/FilterInvoice",
             data: dataToSend,
             success: function (data) {   
                $("#Index").html(data);                     
             }
[HttpPost]                     // Selected DDL value 
public ActionResult FilterInvoice(int idDate)
{       
     switch (idDate)
    { 
        case 0:
             date = DateTime.Parse("01-01-1754");
             break;

        case 3:
             date = DateTime.Now.AddMonths(-12);
             break;
     }
     //var data is returning my expected results 
     var data = new UserBAL().GetInvoice(date);

    // I know this isn't right and needs to be changed 
     return View(data);

我的 ajax 成功函数也没有做任何事情所以我猜这需要一些调整。这也是我使用表格标签显示表格的方式。请记住,我遗漏了一些代码,但所有重要的东西都在这里,唯一的问题是将过滤后的结果渲染回视图,

  @foreach (var item in Model) {
  <tr><td>   
    @Html.DisplayFor(modelItem => item.Invoice_Number)        
    @Html.DisplayFor(modelItem => item.Amt_Total)
</td>
4

2 回答 2

1

您可以将部分视图作为字符串返回,而不是传递视图,然后使用 jquery 在 ajax 成功中更新结果:

控制器逻辑:

[HttpPost]
public JsonResult FilterInvoice(int idDate)
{     
 .....  
 return Json((RenderRazorViewToString("YourViewName", data)), JsonRequestBehavior.AllowGet);
}


    [NonAction]
    public string RenderRazorViewToString(string viewName, object model)
    {
        ViewData.Model = model;
        using (var sw = new StringWriter())
        {
            var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
            var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
            viewResult.View.Render(viewContext, sw);
            viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
            return sw.GetStringBuilder().ToString();
        }
    }

阿贾克斯调用:

    $.ajax({
    //........
    success: function (result) {
        $("#Index").replaceWith(result);
    }
});
于 2013-09-16T17:23:45.593 回答
0

如果其他人遇到此问题,这就是答案。这就是我最终做的事情,行被过滤通过我传递给函数 URL 的日期参数。在 Ajax 调用中填充 Grid 似乎也是一个问题,所以我不得不把它拿出来。

 public JsonResult JqGrid(int idDate)
    {
         switch (idDate)

         #region switch date
            --Switch Statement--
        #endregion
        var invoices = new UserBAL().GetInvoice(date);

        return Json(invoices, JsonRequestBehavior.AllowGet);
    }

    [HttpPost]  // pretty much does nothing, used as a middle man for ajax call 
    public JsonResult JqGridz(int idDate)
    {
        switch (idDate)
        #region switch date

          --Switch Statement--
        #endregion

        var invoices = new UserBAL().GetInvoice(date);

        return Json(invoices, JsonRequestBehavior.AllowGet);
    }

是的,这两个功能看起来非常多余,而且确实如此。我不知道为什么我的帖子不会更新数据,但我每次都需要重新加载网格,当我这样做时,它会调用第一个函数。所以是的,帖子 jqGridz 有点像一个中间人。

这是我使用的 jquery 代码

var dropdown
var Url = '/Invoice/JqGrid/?idDate=0'  
         $(document).ready(function () {

$("#jqgrid").jqGrid({ 
    url: Url,
    datatype: 'json',
    mtype: 'GET', //insert data from the data object we created above 
    width: 500,  
    colNames: ['ID','Invoice #', 'Total Amount', 'Amount Due', 'Amount Paid', 'Due Date'], //define column names
    colModel: [
    { name: 'InvoiceID', index: 'Invoice_Number', key: true, hidden: true, width: 50, align: 'left' },
    { name: 'Invoice_Number', index: 'Invoice_Number', width: 50,  align: 'left'},
    { name: 'Amt_Total', index: 'Amt_Total', width: 50, align: 'left' },
    { name: 'Amt_Due', index: 'Amt_Due', width: 50, align: 'left' },
    { name: 'Amt_Paid', index: 'Amt_Paid', width: 50, align: 'left' },
    { name: 'Due_Date', index: 'Due_Date', formatter: "date", formatoptions: { "srcformat": "Y-m-d", newformat: "m/d/Y" }, width: 50, align: 'left' },

    ],                     
    pager: jQuery('#pager'), 
    sortname: 'Invoice_Number',  
    viewrecords: false, 
    editable: true,
    sortorder: "asc",  
    caption: "Invoices",       
});
$("[name='DDLItems']").change(function () {
    var selection = $(this).val();
     dropdown = {
        //holds selected value 
        idDate: selection
    };

    $.ajax({

        type: "POST",
        url: "Invoice/JqGridz",
        data: dropdown,
        async: false,
        cache: false,
        success: function (data) {         
            $("#jqgrid").setGridParam({ url: Url + selection})               
             $("#jqgrid").trigger('reloadGrid');
        }
    })      

  })
});
于 2013-09-20T19:42:47.880 回答