简体   繁体   English

Ajax在MVC4中无法正常工作?

[英]Ajax is not working correctly in MVC4?

Hi i like to explain my issue with example. 嗨,我想举例说明我的问题。 I have 2 fields FromDate ,ToDate and one button in my view. 我的视图中有2个字段FromDate,ToDate和一个按钮。 if i select the FromDate ,ToDate and click the button means it want to display the data in the same view based on Date Selection which is mentioned in the below image. 如果我选择FromDate,ToDate并单击按钮,则表示它要基于下图中提到的Date Selection在同一视图中显示数据。

My main View 我的主要观点

My View Model (VisitorsViewModel) 我的视图模型(VisitorsViewModel)

    public DateTime  ? FromDate { get; set; }
    public DateTime  ? ToDate { get; set; }

    public ICollection<View_VisitorsForm> Visits { get; set; }

My Controller 我的控制器

   public ActionResult DailyVisitReport()
  {
     return View();
  } 
  public ActionResult GetDatesfromFromDateToDate(string fromDate, string toDate)
    {
        DateTime fromdt = Convert.ToDateTime(fromDate);
        DateTime todt = Convert.ToDateTime(toDate);
        List<View_VisitorsForm> VisitCount = (from v in db.View_VisitorsForm
                                         where v.VisitingDate >= fromdt && v.VisitingDate <= todt
                                         select v).ToList();

        VisitorsViewModel visitotsVM = new VisitorsViewModel();
        visitotsVM.Visits= VisitCount;
        return PartialView("_Visitors", visitotsVM);
    }

Here i got the From Date and To Date Value and list the data using query as per FromDate and ToDate Criteria and passed to partial view 在这里,我得到了“起始日期”和“截止日期”值,并根据“ FromDate”和“ ToDate Criteria”使用查询列出了数据,并传递给部分视图

MY partial view Coding 我的局部视图编码

 @model Sample_Customer.Models.VisitorsViewModel
 @foreach (var item in Model.Visits)
 {
    <tr>
    <td>@Html.DisplayFor(m => item.VisitingDate)</td>
    <td>@Html.DisplayFor(m=>item.Employee)</td>
    <td>@Html.DisplayFor(m=>item.CustomerName)</td>
    <td>@Html.DisplayFor(m=>item.POVisit)</td>
    <td>@Html.DisplayFor(m=>item.ContactPerson)</td>
    <td>@Html.DisplayFor(m=>item.Description)</td>
    <td>@Html.DisplayFor(m=>item.NextAppointment)</td>

     </tr>
 }

My View 我的观点

  @Html.LabelFor(model => model.FromDate)
  @Html.TextBoxFor(model => model.FromDate, new { @class = "form-control", type = "text" })
  @Html.LabelFor(model => model.ToDate)
  @Html.TextBoxFor(model => model.ToDate, new { @class = "form-control", type = "text" }) 
  <input id="Button1" type="button" value="Ok" />

My j query code 我的j查询代码

 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
 <link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothness/jquery-ui.css" />
 <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>

   <script type="text/javascript">
    $(function () {
    $("#FromDate").datepicker({
        dateFormat: "dd/mm/yy",
        changeMonth: true,
        changeYear: true,
    });
    $("#ToDate").datepicker({
        dateFormat: "dd/mm/yy",
        changeMonth: true,
        changeYear: true,
    });
    });
  </script>


  $('#Button1').click(function () {
    debugger;
    alert("hhh");
    $.ajax(
        '@Url.Action("GetDatesfromFromDateToDate", "Report")', {
            type: "GET",
            dataType: "json",
            async: false,
            data: {
                fromDate: $("#FromDate").val(), toDate: $("#ToDate").val()
            },
            error: function (ex) {
                alert('Failed to retrieve Email.' + ex);
            },
            beforeSend: function () {
            },
            success: function (data) {

                var div = $("#visitors");
                div.empty();
                div.html(data);
            }
          });
       });

Now it pass the date to controller and it calculate the list from partial view as per that criteria . 现在,它将日期传递给控制器​​,并根据该条件从局部视图计算列表。 Up to this it is working fine but it wont displaying the data. 到目前为止,它工作正常,但不会显示数据。 see the below image . 见下图。 it getting the data and it is visible in network . 它获取数据并且在网络中可见。 But it not displaying the data. 但是它不显示数据。 it showing fail to retrieve alert box. 它显示无法检索警报框。

Wrong output 输出错误

Please any one tell me the solution to resolve this issue . 请任何人告诉我解决此问题的解决方案。 I tried my level best to explain my issue .please any one understand and give me the solution.please any one check my j query success function code. 我尽力解释了我的问题。请任何人理解并提供解决方案。请任何人检查我的j查询成功功能代码。

You are returning html not json . 您返回的是html而不是json Change this line: 更改此行:

dataType: "json"

to

dataType: "html",

Also this is deprecated way of doing ajax in jQuery. 同样,这是在jQuery中执行ajax不建议使用的方法。 Try this: 尝试这个:

$.ajax({ cache: false,
    url: '@Url.Action("GetDatesfromFromDateToDate", "Report")',
    type: "GET",
    dataType: "html",
    async: false,
    data: {
        fromDate: $("#FromDate").val(), toDate: $("#ToDate").val()
    },
}).done(function (data) {
    var div = $("#visitors");
    div.empty();
    div.html(data);
}).fail(function (jqXHR, textStatus) {
    alert(textStatus);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM