我正在尝试过滤“语言”下拉列表中所选索引更改的“索引”视图数据。我已经编写了一个jquery代码来回发语言下拉列表的选定索引更改,它将调用“索引”操作方法,根据所选语言过滤数据。操作方法是将过滤后的数据返回到视图,但输出仍然是dafault数据(显示所有语言的所有结果。下面是我的代码。我在这里做错了什么?
我的行动方法:
[HttpGet]
public ActionResult Index(string langId)
{
List<Movie> movies = new List<Movie>();
if (langId != null)
{
movies = db.Movies.Where(p => p.Language == langId).ToList();
}
else
movies = db.Movies.ToList<Movie>();
ViewBag.Languages = GetLanguages();
return View(movies);
}
我的观点:
<script language="javascript" type="text/javascript">
$(document).ready(function () {
$("#drpLanguage").change(function () {
debugger;
var drpVal = this.value;
$.ajax({
type: 'GET',
url: '@Url.Action("Index", "Movies")' ,
data: { 'langId': $('#drpLanguage').val() },
success: function (result) {
},
error: function (ts) {
debugger;
alert(ts.responseText)
}
});
// e.preventDefault();
});
});
</script>
<script src='http://code.jquery.com/jquery-latest.min.js' type='text/javascript'></script>
@model IEnumerable<MVCMovies.Models.Movie>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<div> Language:@Html.DropDownList("drpLanguage", new SelectList(ViewBag.Languages, Model), new { id = "drpLanguage" })</div>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.MovieName)
</th>
<th>
@Html.DisplayNameFor(model => model.Language)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.MovieName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Language)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.MovieId }) |
@Html.ActionLink("Details", "Details", new { id=item.MovieId }) |
@Html.ActionLink("Delete", "Delete", new { id=item.MovieId })
</td>
</tr>
}
</table>