-1

I have a view which I want to change its data by changing a DropDownList. Everything works fine but the View doesn't render with new set of data. In other word when I post data to an action method in controller it cannot return to view. here is my code:

View:

    @model IEnumerable<Model.Params>
    <table>
       @foreach(var item in Model)
       {
         <tr>
           <td>
                @Html.DisplayFor(modelItem => item.Name)
          </td>
         </tr>
       }
   </table>
    <div>
        @(Html.Kendo().DropDownList()
              .Name("datetime")
              .DataTextField("DateText")
              .DataValueField("DateText")
              .Events(e => e.Change("datesChange"))
              .DataSource(ds => ds.Read("Read_Intervals", "MyController"))
              )
    </div>

<script>
    function datesChange() {
        var value = $("#datetime").val();
        $.post("@Url.Action("Kq_Read", "MyController")" + "?date=" + value);
    }
</script>

Controller:

public ActionResult Kq(string date)
{
    IQueryable<Params> kq;
    if (date != null)
    {                  
        var datetime = Convert.ToDateTime(date);
        kq = db.Params.Where(p => p.LogDate == datetime);
    }
    else
    {
        var datetime = GetLatestInterval();
        kq = db.Params.Where(p => p.LogDate == datetime);
    }
    return View("Kq", kq);
}

[HttpPost]
public ActionResult Kq_Read(string date)
{
    var datetime = Convert.ToDateTime(date);
    var kq = db.Params.Where(p => p.LogDate == datetime);
    return View("Kq", kq);
}
tereško
  • 58,060
  • 25
  • 98
  • 150
Majid Shahabfar
  • 4,010
  • 2
  • 28
  • 36
  • DataSource(ds => ds.Read("Read_Intervals", "MyController") -- are you modifying Read_intervals? – Nanda Jul 08 '13 at 08:46
  • no Read_Intervals just populates the DropDownList. my problem is that View cannot be opened (Rerendered) from Kq_Read controller action method. – Majid Shahabfar Jul 08 '13 at 10:05
  • Please post the entire View. There is no form and submit button in the View you've posted here, so it's not easy to tell where the issue is. – ataravati Jul 08 '13 at 13:50
  • didn't you see the script function datesChange()! that is the jQuery function which post the selected item from DropDownList to controller action method. by calling that action method I want the entire View be rendered. – Majid Shahabfar Jul 08 '13 at 20:11

1 Answers1

1

You need to do do something with the returned data. When you submit an AJAX request, you need to implement a callback method that will take the returned data and update your UI with it or whatever. That's all on you, the page doesn't just automatically re-render for you.

$.post("@Url.Action("Kq_Read", "MyController")" + "?date=" + value, function (result) {
    // do something with `result`
});
Chris Pratt
  • 232,153
  • 36
  • 385
  • 444