简体   繁体   English

通过 ASP.NET MVC Core 3.1 中的 jQuery 附加 html 以获取部分视图的问题

[英]Issue with appending html for partial view via jQuery in ASP.NET MVC Core 3.1

I seem to be unable to find an answer to this issue, so here it is:我似乎无法找到这个问题的答案,所以这里是:

I created an ASP.NET MVC Core 3.1 Entity Framework Code-First Migrations application.我创建了一个 ASP.NET MVC Core 3.1 实体框架代码优先迁移应用程序。 In the Create view for one of the entities, I render a Partial View that I created:在其中一个实体的 Create 视图中,我呈现了我创建的 Partial View:

        <div class="container" id="Things">
            <partial name="_CreateThingPartial" for="Things[0]" />
        </div>

This renders just fine.这渲染得很好。

I want to append another rendering of the same partial view in that container with the click of a button.我想 append 通过单击按钮在该容器中再次呈现相同的局部视图。 Here is my current jQuery:这是我目前的 jQuery:

var y = 0;

    $('#addThing').click(function () {
        y = y + 1;
        var newDiv = '<partial name="_CreateThingPartial" for="Things[' + y + ']" />';
        $('#Things').append(newDiv);
    });

But with this, clicking the button is doing nothing, and I can't figure out why.但是有了这个,点击按钮什么也没做,我不知道为什么。

If I simply add that html line in the view, like this:如果我只是在视图中添加 html 行,如下所示:

        <div class="container" id="Things">
            <partial name="_CreateThingPartial" for="Things[0]" />
            <partial name="_CreateThingPartial" for="Things[1]" />
        </div>

That renders fine.这很好。

If I set the newDiv like this:如果我这样设置 newDiv:

var newDiv = '<input type="button" value=' + y + ' />';

That works fine!这很好用!

So it seems to only be an issue with the use of the partial view via jQuery.所以这似乎只是通过 jQuery 使用部分视图的问题。

partial is not a html tag but is related to how the .net razor template renders a partial view. partial 不是 html 标记,但与 .net razor 模板如何呈现局部视图有关。 So the actual HTML renderd on the page will not be partial but the content of the partial file _CreateThingPartial.所以页面上实际渲染的 HTML 不会是局部的,而是局部文件 _CreateThingPartial 的内容。

However when you modify the page with jQuery it is done in the frontend and the backend logic of your razor template will not be known.但是,当您使用 jQuery 修改页面时,它是在前端完成的,您的 razor 模板的后端逻辑将是未知的。 So when you try and add a tag called partial it will not be allowed because in the frontend that is not a valid tag.因此,当您尝试添加一个名为 partial 的标签时,它不会被允许,因为在前端这不是一个有效的标签。

To get a better understanding of how the .net razor template renders to the frontend i would sugest you open the view source or inspection page.为了更好地了解 .net razor 模板如何呈现到前端,我建议您打开查看源代码或检查页面。

<partial> is not a HTML tag. <partial>不是 HTML 标记。 it's a server-side tag helper defined by .NET Core.它是由 .NET Core 定义的服务器端标签助手。

Consider your Things entities.考虑您的Things实体。 It's stored in the database.它存储在数据库中。 Unless you render all the entities in your.cshtml file, you can't have them in client side without sending additional requests to your server.除非您在您的 .cshtml 文件中呈现所有实体,否则您不能将它们放在客户端而不向您的服务器发送额外的请求。

<partial> tag helper is executed when rendering the resultant HTML markup at the server , while jQuery is executed at the client (eg browser). <partial>标签助手在服务器渲染生成的 HTML 标记时执行,而 jQuery在客户端(例如浏览器)执行。 That's why you cannot simply put the <partial> inside your jQuery and get the data you want.这就是为什么您不能简单地将<partial>放在 jQuery 中并获取您想要的数据。

Possible solution可能的解决方案

The following code snippets show you the idea on how to handle this problem.以下代码片段向您展示了如何处理此问题的想法 Copy-and-paste directly does not work.直接复制粘贴是不行的。 You have to understand the concept and do some google search in order to make it works.您必须了解这个概念并进行一些谷歌搜索才能使其正常工作。

You can define a partial action in your controller:您可以在 controller 中定义部分操作

public ActionResult GetThingPartial (int id)
{
    /* get Thing[id] from database using EF Core */
    var videModel = Thing[id];
    return PartialView( "_CreateThingPartial", viewModel );
}

Fetch the partial result using jQuery's ajax call.使用 jQuery 的 ajax 调用获取部分结果。

In your.cshtml file:在您的 .cshtml 文件中:

var url = '@Url.Action("GetThingPartial", "YourControllerName")';

$.get(url, function(data) {
    /* append the data, which should be the partial HTML, to the corresponding <div> */
});

This article may also helpful to solve the problem you met: https://www.c-sharpcorner.com/uploadfile/manas1/sending-partialview-using-jquery-ajax-request-in-asp-net-mvc/本文也可能有助于解决您遇到的问题: https://www.c-sharpcorner.com/uploadfile/manas1/sending-partialview-using-jquery-ajax-request-in-asp-net-mvc/

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

相关问题 无法通过ASP.NET Core中的JQuery ActionResult调用加载部分视图 - Unable to load Partial View via JQuery ActionResult call in ASP.NET Core ASP.NET Core MVC 自动完成部分视图未显示结果 - ASP.NET Core MVC autocomplete partial view not showing result 在 ASP.NET Core MVC 的 iframe 中显示局部视图 - Display partial view in an iframe in ASP.NET Core MVC 在 ASP.NET Core 2.2 中将局部视图渲染为 HTML 字符串 - Render Partial View to HTML string in ASP.NET Core 2.2 提交表单时ASP.NET MVC部分视图问题 - ASP.NET MVC Partial View issue when Submitting a Form 具有部分视图的ASP.NET MVC3 JQuery对话框 - ASP.NET MVC3 JQuery dialog with partial view 在ASP.NET MVC C#中使用Jquery更新部分视图 - Updating partial view with Jquery in ASP.NET MVC C# 问题,VS 19 ASP.NET Core 3.1 当我开始创建“MVC Controller with view, using Entity Framework” - Issue, VS 19 ASP.NET Core 3.1 when I start to create “MVC Controller with view, using Entity Framework” ASP.Net Core 3.1 - 使用 Ajax 将值传递给部分视图模态? - ASP.Net Core 3.1 - passing value to partial view Modal with Ajax? ASP.NET Core 3.1 - 将子项添加到 razor 局部视图 - ASP.NET Core 3.1 - adding child item to razor partial view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM