asp.net mvc从另一个无法正常工作的方法返回视图

时间:2018-02-28 22:12:40

标签: javascript c# asp.net-mvc razor

我在加载页面时构建我的MVC索引视图。 我的观点:

<div class="container">
    <br/>
    <div class="row">
        <label for="dateRange" style="margin-left: 240px;">Date Range:</label>&nbsp;&nbsp;
        <input class="form-control col-md-2" type="text" id="startDate" value="@ViewBag.StartDate" />&nbsp;&nbsp;
        <label for="dateRange"> - </label>&nbsp;&nbsp;
        <input class="form-control col-md-2" type="text" id="endDate" value="@ViewBag.EndDate" />&nbsp;&nbsp;
        <button type="button" class="btn btn-success" onclick="filterData();">Submit</button>
    </div>
    <br/>
    <table class="table table-bordered">
        <thead>
            <tr>
                <th>Payment Date</th>
                <th>Tenant</th>
                <th>Payment Amount</th>
                <th>Payment Category</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model)
            {
                <tr>
                    <td>@item.PaymentDate.ToString("MM/dd/yyyy")</td>
                    <td>@item.TenantFullName</td>
                    <td>@string.Format("{0:C}", @item.PaymentAmount)</td>
                    <td>@Html.ActionLink(@item.PaymentCategory, "Index", "AddEditCategory", new { id = @item.CategoryID, tID = @item.TenantTransactionID }, null)</td>
                </tr>
            }
        </tbody>
    </table>
</div>

<script type="text/javascript">
        // When the document is ready
        $(document).ready(function () {
            $('#startDate').datepicker({
                format: "mm/dd/yyyy"
            });
            $('#endDate').datepicker({
                format: "mm/dd/yyyy"
            });
        });
</script>

filterData()方法的JavaScript:

    function filterData() {
    if ($("#startDate").val() == "" || $("#endDate").val() == "") {
        $("#msgError").html("Please enter the date range")
        $("#defaultErrors").modal();
    } else {
        data = {
            StartDate: $("#startDate").val(),
            EndDate: $("#endDate").val()
        }
        $.ajax({
            url: '/AllocateTenantPayments/filter',
            type: 'POST',
            data: JSON.stringify({
                dateRange: data
            }),
            contentType: 'application/json; charset=utf-8',
            success: function (data) {

            },
            error: function (erro) {
                $("#msgError").html("Error processing your request. We'll contact you shortly.")
                $("#defaultErrors").modal();
            }
        });
    }
}

我的控制器加载索引数据,方法过滤器使用新的日期范围刷新它:

    public ActionResult Index(int? id)
{
    if (id == null)
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

    System.Web.HttpContext.Current.Session["PropertyID"] = id;

    // Query data and set the viewModel

    ViewBag.StartDate = startDate.ToString("MM/dd/yyyy");
    ViewBag.EndDate = endDate.ToString("MM/dd/yyyy");
    return View(viewModel);
}

public ActionResult filter(DateFilter dateRange)
{
    // query new data and set the viewModel

    ViewBag.StartDate = dateRange.StartDate.ToString("MM/dd/yyyy");
    ViewBag.EndDate = dateRange.EndDate.ToString("MM/dd/yyyy");
    return View("~/Views/AllocateTenantPayments/Index.cshtml", viewModel);
}

当我调试控制器方法时,哪一个返回我想要的viewModel数据是正确的;但是,filter()方法不会刷新浏览器中的视图。它坚持返回的第一个数据。 有谁知道我做错了什么?

由于

0 个答案:

没有答案