MVC jQuery从Ajax检索参数

时间:2014-09-29 16:27:40

标签: jquery ajax asp.net-mvc

我第一次尝试PartialView。

我的计划如下:

页面上的那个将显示2个日期选择器字段(从和到),旁边有一个搜索按钮。当用户点击时,控制器构建结果集,它将出现在 divResults 中。

这是我的页面,

// /Home/Reports.cshtml

@using (@Html.BeginForm("Report", "Account", FormMethod.Post, new { id = "reportform" })) {
<fieldset>
    <legend>Filter</legend>
    <div class="editor-label">
        @Html.Label("Date Interval lower bound")
    </div>
    <div class="editor-field">
        <input id="date_from" type="date" class="datefield" />
    </div>
    <div class="editor-label">
        @Html.Label("Date Interval upper bound")
    </div>
    <div class="editor-field">
        <input name="date_to" type="date" class="datefield" />
    </div>
    <button id="btnsearch">Search</button>
</fieldset>
}
<div id="divResults">
</div>
<script type="text/javascript">
$(function () {
    $('#btnsearch').on("Click", function (e) {
        var date_from = $("#date_from").val();
        var date_to = $("#date_to").val();
        $.ajax({
            url: '/Home/Report',
            contentType: 'application/html; charset=utf-8',
            type: 'POST',
            dataType: 'html',
            data: { datefrom: date_from, dateto: date_to }
        })
        .done(function (partialViewResult) {
            $("#divResults").html(partialViewResult);
        });
        e.preventDefault();
    });
});
</script>

这是我的HomeController.cs

[HttpGet]
public ActionResult Report() {
  return View();
}
[HttpPost]
public ActionResult Report(string datefrom, string dateto) {
  return null;
}

即使 date_from date_to 包含所需的值,第二个(HttpPost)方法也不会捕获它们。

看起来没事,但它不起作用。

你有什么想法吗?

由于

0 个答案:

没有答案