javascript - 在 ASP.Net Core MVC View 中刷新 ViewComponent

标签 javascript asp.net-mvc asp.net-core asp.net-core-viewcomponent

我有一个 View 组件,EventsViewComponent,它使用以下代码行加载到我的事件 View index.cshtml 中:

<div id="events">
    @await Component.InvokeAsync("Events", new { showPrevious = Model.ShowPrevious, showUpcoming = Model.ShowUpcoming })
</div>

我添加了两个复选框,如下所示:
@Html.CheckBoxFor(m => m.ShowPrevious, new { id = "cbShowPrevious", onchange = "ReloadEvents()" })
@Html.CheckBoxFor(m => m.ShowUpcoming, new { id = "cbShowUpcoming", onchange = "ReloadEvents()" })

ReloadEvents() 指的是一个 Javascript 函数,我希望在其中使用 Ajax 调用来刷新 EventsViewComponent,例如:
function ReloadEvents() {
    $.ajax({
        url: '@Url.Action("ReloadEvents", "Events")',
        data: {
            showPrevious: document.getElementById("cbShowPrevious").checked,
            showUpcoming: document.getElementById("cbShowUpcoming").checked
        },
        success: DoThis()
    })
}

function DoThis() {
    const eventsDiv = document.getElementById('events');
    eventsDic.innerHTML = //HTML from EventsViewComponent
}

但我似乎无法从 EventsViewComponent 获取 HTML。

我已经为 EventsViewComponent 编写了 Default.cshtml,如下所示:
@{
    List<Event> events = ViewData["Events"] as List<Event>;
    if (events.Count > 0)
    {
        <table>
            //event data from the model
        </table>
    }
}

EventsViewComponent 中的 InvokeAsync 方法被击中,EventsController 中的 ReloadEvents 方法也是如此,但我显然误解了一些东西,因为我似乎无法更新 EventsViewComponent。

请有人建议这是否可能以及如何实现它?

最佳答案

要从 EventsViewComponent 获取 HTML,您需要进行如下更改:

success: function (data) {
        $("#events").html(data);
}

这是一个完整的工作演示,如下所示:

1.型号:
public class Event
{
    public bool ShowPrevious { get; set; }
    public bool ShowUpcoming { get; set; }
    public string Data { get; set; }
}

2. View 组件:
public class EventsViewComponent : ViewComponent
{
    List<Event> data = new List<Event>() {
        new Event(){ ShowPrevious=true,ShowUpcoming=false,Data="aaa"},
        new Event(){ ShowPrevious=false,ShowUpcoming=true,Data="bbb"},
        new Event(){ ShowPrevious=false,ShowUpcoming=true,Data="ccc"},
    };
    public IViewComponentResult Invoke(bool showPrevious,bool showUpcoming)
    {
        if (showPrevious == true && showUpcoming == true)
        {
            ViewData["Events"] = data;
        }
        else if (showPrevious)
        {
            ViewData["Events"] = data.Where(u => u.ShowPrevious == true).ToList();
        }
        else if(showUpcoming)
        {
            ViewData["Events"] = data.Where(u => u.ShowUpcoming == true).ToList();
        }
        return View();
    }
}

3. Controller :
public class HomeController : Controller
{

    public IActionResult ReloadEvents(bool showPrevious, bool showUpcoming)
    {
        return ViewComponent("Events", new { showPrevious = showPrevious, showUpcoming = showUpcoming });
    }
    public IActionResult Index()
    {
        var model = new Event() { ShowPrevious = true, ShowUpcoming = true };
        return View(model);
    }
}

4.Index.cshtml:
@model Event
@Html.CheckBoxFor(m => m.ShowPrevious, new { id = "cbShowPrevious", onchange = "ReloadEvents()" })
@Html.CheckBoxFor(m => m.ShowUpcoming, new { id = "cbShowUpcoming", onchange = "ReloadEvents()" })
<div id="events">
    @await Component.InvokeAsync("Events", new { showPrevious = Model.ShowPrevious, showUpcoming = Model.ShowUpcoming })
</div>

@section Scripts
{
<script>
    function ReloadEvents() {
        $.ajax({
        url: '@Url.Action("ReloadEvents", "Home")',
        data: {
        showPrevious: document.getElementById("cbShowPrevious").checked,
        showUpcoming: document.getElementById("cbShowUpcoming").checked
            },
            success: function (data) {
                $("#events").html(data);
            }
        })
} 
</script> 
}

5.Default.cshtml( View 组件Razor View ):
@model Event
@{
    List<Event> events = ViewData["Events"] as List<Event>;
    if (events.Count > 0)
    {
        <table>
            <tr>
                <th>ShowPrevious</th>
                <th>ShowUpcoming</th>
                <th>Data</th>
            </tr>
            <tbody>
                @foreach (var item in events)
                {
                    <tr>
                        <td>@item.ShowPrevious</td>
                        <td>@item.ShowUpcoming</td>
                        <td>@item.Data</td>
                    </tr>
                }

            </tbody>
        </table>
    }
}

结果:
enter image description here

关于javascript - 在 ASP.Net Core MVC View 中刷新 ViewComponent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60608174/

相关文章:

javascript - 更改检测后在 Canvas 上绘制 angular2 图像

javascript - JIT Spacetree 将标签保存为图像

asp.net-mvc - 将调试器附加到 IIS 工作进程时出错

c# - 我的 .NET Core 3.1 应用程序可以移植到 .NET 5 吗?

javascript - 电子邮件黑名单和验证

javascript - CSS和/或Javascript中给定文本中俄语字母表的每个字符的不同颜色

asp.net-mvc - 获取 "The JSON request was too large to be deserialized"

asp.net - 在嵌套对象上调用 MVC IValidatableObject

c# - MvcOptions.InputFormatters 不适用于 asp.net vnext beta7

c# - WebSocket 握手错误 : Unexpected response code 400