简体   繁体   English

如何异步更新ASP.NET MVC部分视图?

[英]How do I update ASP.NET MVC partial view asynchronously?

I am trying to asynchronously update a table of data based on the user's selection from a drop-down. 我试图根据用户从下拉列表中的选择异步更新数据表。 Using stuff I've read here and elsewhere, I've been trying to use a partial view to accomplish this. 使用我在这里和其他地方读过的东西,我一直在尝试使用局部视图来实现这一目标。 My code looks something like this: 我的代码看起来像这样:

Home.cshtml: Home.cshtml:

<body>
<div id="main-region">       
    <div class="mySelector">
        <label class="dropdown-label">Filter: </label>
        @Html.DropDownListFor(m => m.SelectedStatus, Html.GetEnumSelectList(typeof(MyEnum)),
       new { @id = "mySelector" })
    </div>
    <div id="myPartialView">
        @Html.Partial("_Summary", Model)
    </div>
</div>
<script type="text/javascript" src="~/lib/jquery/dist/jquery.min.js"></script>
<script type="text/javascript">
    $('#mySelector').change(function () {
        var newSelectionID = $(this).val();
        $('#myPartialView').load('@Url.Action("Filter", "Home")') //syntax for including newSelectionID?
    });

</script>

_Summary.cshtml is just a simple table. _Summary.cshtml只是一个简单的表。

HomeController.cs: HomeController.cs:

    [HttpGet]
    public IActionResult Filter(string statusID)
    {
      var modelData = mService.GetData(statusID);
        return PartialView("_Summary", model);
    }

I'm stuck on two things: 我坚持两件事:

  • I can't even seem to get this to hit my Filter method server-side. 我甚至无法得到这个来点击我的Filter方法服务器端。
  • I'm not sure how to pass back the user-selected value. 我不知道如何传回用户选择的值。

I'm obviously missing something. 我显然错过了一些东西。 Can anyone help me out? 谁能帮我吗? I'm using asp.net core. 我正在使用asp.net核心。

Thanks very much. 非常感谢。

Your code might be crashing because you are not passing the statusID parameter to your method and i see you are using the parameter value to get the model data needed for the partial view. 您的代码可能会崩溃,因为您没有将statusID参数传递给您的方法,我发现您正在使用参数值来获取部​​分视图所需的模型数据。 If the code crashes, the server will return a 500 Internal error status code in the response and the div ( myPartialView ) will not be updated with the content. 如果代码崩溃,服务器将在响应中返回500内部错误状态代码,并且不会使用内容更新div( myPartialView )。

Since your action method is decorated with GET attribute, you may pass the value in querystring. 由于您的action方法是使用GET属性修饰的,因此您可以传递querystring中的值。

This should work 这应该工作

$(function() {

    $('#mySelector').change(function() {
        var v = $(this).val();
        $('#myPartialView').load('@Url.Action("FilterOnStatus", "Home")?statusID=' + v);
    });

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 asp.net mvc更新部分视图 - asp.net mvc update Partial view 如何将视图和部分视图返回到ASP.NET MVC 4中的索引布局? - How do I return a View and a Partial View to the Index Layout in ASP.NET MVC 4? 如何将带有自己的控制器的局部视图添加到 asp.net mvc 4 中的另一个视图 - How do I add a partial view with its own controller to another view in asp.net mvc 4 如何使用带有 ASP.NET MVC 的局部视图显示数据库表中的所有项目? - How do I display all items from a table in a database using a partial view with ASP.NET MVC? 部分视图不会在 asp.net MVC 中使用 ms ajax 更新 - Partial view does not Update with ms ajax in asp.net MVC 我如何制作一个多用途的局部视图,以在asp.net mvc 2中呈现具有不同名称值的控件? - How do I make a multi-use partial view that renders controls with different name values in asp.net mvc 2? 从另一个局部视图更新部分视图 - ASP.NET MVC2 - Update A Partial View From Another Partial View - ASP.NET MVC2 我如何在asp.net mvc中更新查询字符串 - how do i update a querystring in asp.net mvc 如何在 ASP.NET MVC 视图中对数据进行分组? - How do I group data in an ASP.NET MVC View? 如何在部分视图(ASP.NET MVC)中使用DevExtreme? - How to use DevExtreme in partial view (ASP.NET MVC)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM