我对 MVC 很陌生,刚刚遇到一个需要帮助的场景。我正在使用 MVC 4 在 ASP.NET、C#.NET 中创建一个网站。所以根据我的要求,我有两个 DropDowns<select>
标记在我的cshtml
页面。
1) selectUniversity
2) selectCollege
我有一个名为的数据库表College_table
,其字段是:
1) Univ_ID
2) Univ_Name
3) College_ID
4) College_Name
5) College_Address
6) College_Contact
现在,在我selectUniversity
的列表中,我列出了所有大学名称,并且selectCollege
所有大学名称都属于selectUniversity
下拉列表中选择的大学。并且 WebGrid 内容将根据上述任何下拉列表的选择而改变。
我的代码更改: 查看 Javascript:
<script type="text/javascript">
function ajaxCallGetColleges() {
var e = document.getElementById("selectUniversity");
var strUniv = e.options[e.selectedIndex].value;
$.ajax({
url: '@Url.Action("GetCollegesActionResult", "CollegeController")',
type: 'POST',
data: { 'data': strUniv },
success: function (ResponceObject) {
alert(ResponceObject);//Need to update this data into WebGrid and selectCollege dropdown.
}
});
}
function ajaxCallGetCollegeDetail() {
var e = document.getElementById("selectCollege");
var strCollege = e.options[e.selectedIndex].value;
$.ajax({
url: '@Url.Action("GetCollegeDetailActionResult", "CollegeController")',
type: 'POST',
data: { 'data': strCollege },
success: function (ResponceObject) {
alert(ResponceObject);//Need to update this data into WebGrid.
}
});
}
</script>
CSHTML 代码:
<table>
<tr>
<td>
<select id="selectUniversity" name="selectUniversity" onchange="ajaxCallGetColleges()">
@{
//Logic for adding University names as options to the dropdown dynamically
}
</select>
</td>
<td>
<select id="searchBy" name="searchBy" onchange="ajaxCall()">
<select id="selectUniversity" name="selectUniversity" onchange="ajaxCallGetCollegeDetail()">
@{
//Logic for adding college names as options to the dropdown dynamically
}
</select>
</td>
</tr>
<tr>
<td colspan="2">
<div id="MyGrid">
@{
WebGrid grid = new WebGrid(source: Model,
defaultSort: "Name",
rowsPerPage: 15);
grid.SortDirection = SortDirection.Ascending;
}
@grid.GetHtml(
fillEmptyRows: false,
mode: WebGridPagerModes.All,
firstText: "<< First",
previousText: "< Prev",
nextText: "Next >",
lastText: "Last >>",
columns: new[] {
grid.Column("Univ_ID",header: "Univ ID", canSort: false),
grid.Column("Univ_Name",header: "Univ Name"),
grid.Column("College_ID",header: "College ID", canSort: true),
grid.Column("College_Name",header: "College Name"),
grid.Column("College_Address", header: "College Address", canSort: true),
grid.Column("College_Contact",header: "Contact"),
grid.Column("", header:"Edit", format: (item) => Html.ActionLink("Edit", "Edit", new { univ_ID=item.Univ_ID })),
grid.Column("", header:"Delete", format: (item) => Html.ActionLink("Delete", "Delete", new { univ_ID=item.Univ_ID}, new { onclick="return confirm('Are you sure?');"}))
})
</div>
</td>
</tr>
</table>
控制器 C#.NET 代码:
public ActionResult SearchByBranchStatus(string data)
{
List<CollegesDetail> colleges= _collegeService.GetCollegesDetail();
var RespObject = colleges.Where(c => c.Name == data);
return View(RespObject);
}
public ActionResult GetCollegeDetailActionResult(string data)
{
List<CollegeDetail> colleges= _collegeService.GetCollegeDetail();
var RespObject = colleges.Where(c => c.Name == data);
return View(RespObject);
}
我也浏览了许多 SO 和 MSDN 站点,但没有找到任何帮助。请为我提供任何参考或想法来解决这个问题。我知道与我的问题有关,在许多论坛上提出了很多问题,但我没有得到它们。可能是它最简单的一个,但是当我最近开始研究 MVC 4 时,我对 MVC 4 没有很好的了解。我急切地等待答案。任何帮助将不胜感激。提前致谢..
注意:-目前我指的是MSDN 杂志
更新:- 我已经部分回答了我的问题,即如何通过 MVC 4 中的 Ajax 在 DropDown 选择更改上更新 WebGrid