【发布时间】:2013-04-01 08:51:40
【问题描述】:
我对 MVC 很陌生,刚刚遇到一个需要帮助的场景。我正在使用 MVC 4 在 ASP.NET、C#.NET 中创建一个网站。所以根据我的要求,我有两个 DropDowns在我的cshtml 页面中是<select> 标签。
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 Magazine
更新:- 我已经部分回答了我的问题,即如何通过 MVC 4 中的 Ajax 在 DropDown 选择更改上更新 WebGrid
【问题讨论】:
-
您应该从控制器返回 json,而不是返回视图。此外,您正在从 $.ajax 请求 POST 类型,但您尚未声明您的控制器操作是 [HttpPost]。检查这个post
-
如果没有 [HttpPost],它也会在返回 Content(string) 的情况下给我数据,但在返回 View() 的情况下,它会给我的 Ajax 调用提供错误
-
如果您作为视图返回,它不会以 $.ajax 调用所期望的格式返回,这就是它产生错误的原因。当您返回内容时,它以字符串形式返回,并且 ajax 调用可以智能地将类型检测为字符串,并且它会成功回调
-
尝试像这样返回:
return Json(respObject, JsonRequestBehaviour.AllowGet);在成功回调中放置一个debugger;,看看返回的数据中有哪些可用的选项 -
我现在将模型作为响应对象,但我现在将如何更新/绑定 WebGrid?
标签: c# asp.net-mvc razor asp.net-mvc-4 webgrid