简体   繁体   English

Razor Pages:内部带有局部视图的页面。 如何存储部分视图的数据?

[英]Razor Pages: Page with partialview inside. How can i store my Data for the partialview?

Hello guys i am working on a projekt where i can add articles to a cart.大家好,我正在开发一个可以将文章添加到购物车的项目。 My cart is a partialview.When i add a Article to my cart the old data isnt stored and only one (the new article) article is added to my cart.我的购物车是一个部分视图。当我将一篇文章添加到我的购物车时,旧数据不会存储,并且只有一篇(新文章)文章添加到我的购物车中。

This is the code for the Post in my codebehind Im adding a Postition with RandomNumbers for testing这是我的代码隐藏中的 Post 代码,我添加了一个带有 RandomNumbers 的 Postition 进行测试

    [BindProperty]
    public List<Position> ArticlePositions { get; set; } = new List<Position>();

    public PartialViewResult OnPostAddPosition(List<Position> articlePositions)
    {
        Random myObject = new Random();
        Random myObject1 = new Random();

        articlePositions.Add(new Position
        {
            ArticleAmount = myObject1.Next(10, 100),
            ArticleId = myObject.Next(10, 100)
        });
        var partialViewResult = new PartialViewResult()
        {
            ViewName = "_ArticlePositonsPartial",
            ViewData = new ViewDataDictionary<List<Position>>(ViewData, articlePositions),
        };

        return partialViewResult;
    }

This is my Partialview with the Model:这是我对模型的部分视图:

@model List<Rechnungen.Models.Position>

<div class="tableFixHead">
    <table class="table table-striped text-center table-sm ">
        <thead class="table-dark ">
            <tr>
                <th style="width:72.5px" class="">Anzahl</th>
                <th style="width:72.5px" class="">ID</th>               
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model)
            {
                <tr id="">
                    <td style="width:72.5px" class="pt-2 ">
                        <span class="TBarticleAmount">@item.ArticleAmount</span>
                    </td>
                    <td style="width:72.5px" class="pt-2 ">
                        <span class="TBarticleType">@item.ArticleId</span>
                    </td>               
                </tr>
            }
        </tbody>
    </table>
</div>

Where i render the Partial:我在哪里渲染部分:

  <div id="MyPartialView">
   @{
     await Html.RenderPartialAsync("_ArticlePositonsPartial", Model.ArticlePositions, ViewData);
    }
 </div>

How i add A Position to cart:我如何将位置添加到购物车:

 @Html.AntiForgeryToken()
 <button class="btn btn-outline-secondary"  onclick="GetPartialView()"><i class="fa-solid fa-plus"></i>Hinzufügen</button>

And this is my Javascript for it:这是我的Javascript:

  function GetPartialView() {
        $.ajax({
            type: "POST",
            url: '?handler=AddPosition',
            dataType: "html",
            headers: { "RequestVerificationToken": $('input[name="__RequestVerificationToken"]').val() },
            success: function(data) {
                 $("#MyPartialView").html(data);
            },
            error: function(result) {
                alert("fail");
            }
        });
    }

My code is working so far, when i add something to my cart my site is looking like this=>到目前为止,我的代码正在运行,当我在购物车中添加一些东西时,我的网站看起来像这样 =>

将一篇文章添加到购物车

When i click a second time on the add button my site looks like this=>当我再次单击添加按钮时,我的网站看起来像这样=>

将第二篇文章添加到购物车

So my problem is that the Data in ArticlePositions isnt stored.所以我的问题是没有存储 ArticlePositions 中的数据。 When i Debugg my Code the ArticlePositions are NULL when the Post Method is called.当我调试我的代码时,调用 Post 方法时 ArticlePositions 为NULL

HTTP is stateless, so any state created in your OnPostAddPosition method is destroyed at the end of the request. HTTP 是无状态的,因此在OnPostAddPosition方法中创建的任何状态都会在请求结束时被销毁。 You have to implement strategies to persist state across requests.您必须实施策略以跨请求保持状态。 There are a number of options available in Razor Pages. Razor Pages 中有许多可用选项。 You should research them to identify the most suitable one for your application:您应该研究它们以确定最适合您的应用程序的一种:

https://www.learnrazorpages.com/razor-pages/state-managementhttps://www.learnrazorpages.com/razor-pages/state-management

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM