首页 > 解决方案 > 使用 AJAX 调用带参数的视图

问题描述

所以我有一个AJAX函数,它用参数调用控制器中的视图。当我调试一切正常时,我也得到了正确的结果,但页面没有在浏览器中重新加载。我正在使用将AJAX过滤器应用于列表,但现在视图设置正确,但浏览器中没有重新加载/刷新列表。然后当我自己重新加载时,我得到的列表没有过滤器。(这是有道理的)

例如:我有一个包含猫和狗的列表,并且只为狗应用过滤器,在 ajax 调用完成后,我的列表中仍然有猫。我搜索了很长时间来寻找答案,但没有找到一架喷气式飞机。

在成功函数中添加 alocation.reload();也不起作用,因为随后使用其他参数调用视图,然后在AJAX调用中设置。

所以我只想有一个AJAX调用以p_applicationId作为参数调用视图,它们显示通过调用的视图AJAX

AJAX

 $(document).ready(function ()
{
    $("#applicationDropdown").change(function () {
        var releaseId = $("#applicationDropdown").val();
        var filterReport = $("#filterReport");
         $.ajax({
             type: "POST",
             url: '@Url.Action("Index", "ProductionDeployment")',
             data: { p_applicationId: releaseId },
            success: function (data)
            {
                filterReport.html('Success, The Releases have been filtered.');
            },
            error: function (xhr, ajaxOptions, thrownError)
            {
                filterReport.html('Failed to filter the Releases. ' + thrownError);
                //  alert('Failed to filter the Releases.', xhr, thrownError);
            }
        });
    });
});

生产部署控制器.cs

public class ProductionDeploymentController : BaseController
{
    // Variable Initialisation
    DBhandler dbhandler = new DBhandler();
    string cnnStr = ConfigurationManager.ConnectionStrings["conChdbd1"].ConnectionString;
    string sqlStr;

    /// <summary>
    /// GET: ProductionDeployment
    /// </summary>
    /// <returns> Function Return View </returns>
    public ActionResult Index(String p_applicationId)
    {
        if (p_applicationId == null)
        {
            p_applicationId = "CTTS";
        }
        PrepareViewBagJustApplication();
        List<ProductionPending> pendingDeployments = GetPendingProductionDeployments(p_applicationId);
        ProductionDeployments pd = new ProductionDeployments();
        pd.Pending = pendingDeployments;
        return View(pd);
    }

    /// <summary>
    /// Function contains a SQL which links to a Package, which is sent to the DBHandler and returns a DataTable,
    /// The DataTable then is then converted into a List<> in the ConvertTo...() function and passed back to the View.
    /// </summary>
    /// <returns> List of Pending Productions: List<ProductionPending> </returns>
    private List<ProductionPending> GetPendingProductionDeployments(String p_applicationId)
    {
        List<ProductionPending> list = null;
        sqlStr = "ADM_PORTAL_REP.p_PendingProdDeployments";
        DataTable dt = dbhandler.ExecuteQuery(sqlStr, "2016-04", p_applicationId);
        if (dt.Rows.Count > 0)
        {
            list = ConvertToProductionPending(dt).ToList();
        }
        else
        {
            list = new List<ProductionPending>();
            list.Clear();
        }
        return list;
    }

    /// <summary>
    /// Function to convert the DataTable sent in into an IEnumerable<ProductionPending> 
    /// </summary>
    /// <param name="dt"></param>
    /// <returns> IEnumerable of Pending Productions: IEnumerable<ProductionPending> </returns>
    private IEnumerable<ProductionPending> ConvertToProductionPending(DataTable dt)
    {
        return dt.AsEnumerable().Select(row =>
        {
            return new ProductionPending
            {
                Id = Convert.ToInt32(row["ID"]),
                releaseId = row["releaseId"].ToString(),
                RFCNumber = Convert.ToInt32(row["RFCNumber"]),
                developerInitials = row["developerInitials"].ToString(),
                fileName = row["fileName"].ToString(),
                fileVersion = row["fileVersion"].ToString(),
                RFDNumber = Convert.ToInt32(row["RFDNumber"]),
                desiredDeploymentDate = Convert.ToDateTime(row["desiredDeploymentDate"]),
                deploymentDate = Convert.ToDateTime(row["deploymentDate"]),
                notes = row["notes"].ToString(),
                detailNotes = row["detailNotes"].ToString(),
                approvedInd = row["approvedInd"].ToString(),
                dateRequested = Convert.ToDateTime(row["dateRequested"]),
                applicationId = row["application"].ToString(),
            };
        });
    }
}

标签: c#jqueryajax

解决方案


推荐阅读