【问题标题】:Update WebGrid and DropDown through Ajax in MVC 4在 MVC 4 中通过 Ajax 更新 WebGrid 和 DropDown
【发布时间】: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


【解决方案1】:

Finlay 我已经通过使用局部视图解决了这个问题。现在我填写它非常简单。下面提到了我如何实现的步骤:

  1. 我创建了两个视图(MainView 和 _MainView
  2. _MainView 是部分视图,我将完整的WebGridWebGridModel 绑定在一起。
  3. 现在在MainView 中,我将partialview(_MainView) 放到了一个div 标签中,并用Model(@Html.Partial("_MainView", Model)) 调用了partial view
  4. 在 Ajax 调用中,我按照上面的问题所述调用了 ActionResult,在成功时,我使用返回的部分视图更新了 div 标记
  5. ActionResult 而不是返回完整视图,我返回部分视图以仅更新WebGridreturn PartialView("_MainView", responceObject);)。

我有一个正在运行的示例应用程序。如果有人发现这个答案有任何困难,那么可以问我。我可以在这里分享完整的工作代码。尤其感谢 Karthik 回答我的问题和我所有的 cmets :)

【讨论】:

  • 嗯,你必须回答你自己的问题 :)
  • @SharpUrBrain 你能分享你的代码吗,真的很有帮助
猜你喜欢
  • 2014-12-04
  • 2013-02-23
  • 1970-01-01
  • 1970-01-01
  • 2012-10-18
  • 2012-05-22
  • 1970-01-01
  • 2012-05-14
  • 2016-07-20
相关资源
最近更新 更多