我正在使用MVC 5,我通过ViewData填写州名为Country的下拉列表,并且在我的视图页面中我显示了每个州的记录。但我希望,在视图中选择Dropdown值后只显示选中的状态值。这是我的代码“
”控制器
public ActionResult Index(int? id)
{
ViewData["State"] = new SelectList(db.state, "state_id", "name");
if (id!=null)
return View(db.state.Where(x => x.state_id == id));
else
return View(db.state.ToList());
}
查看
@Html.DropDownList("ddlstate", (SelectList)ViewData["State"], "Select State", new { onchange = "Index(this.value);", @class = "form-control" })
<table class="table" id="statedetails">
<thead>
<tr>
<th>
State ID
</th>
<th>
State Name
</th>
<th>
City
</th>
<th>
Population
</th>
<th>
Education %
</th>
<th></th>
</tr>
</thead>
@foreach (var item in Model)
{
<tbody>
<tr>
<td>
@Html.DisplayFor(modelItem => item.stateid)
</td>
<td>
@Html.DisplayFor(modelItem => item.statename)
</td>
<td>
@Html.DisplayFor(modelItem => item.city)
</td>
<td>
@Html.DisplayFor(modelItem => item.population)
</td>
<td>
@Html.DisplayFor(modelItem => item.eduction)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.offer_id }) |
@Html.ActionLink("Details", "Details", new { id = item.offer_id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.offer_id })
</td>
</tr>
</tbody>
}
</table>
的javascript
<script type="text/javascript">
function Index(id) {
$.ajax({
url: "@Url.Action("Index","State")",
type: "POST",
data: { "id": id }
});
}
</script>