简体   繁体   English

ASP Razor 页面 BindProperties 值在 Post 上丢失

[英]ASP Razor pages BindProperties values lost on Post

I 'm working with an ASP Razor project (.Net5).我正在使用 ASP Razor 项目 (.Net5)。 Thee is a page with the user list and a filter.这是一个包含用户列表和过滤器的页面。 The goal is to present the user that match the filtering criteria.目标是呈现符合过滤条件的用户。 The code below is able to do the filtering but the filtering values are lost (become blank) everytime the filtering is done.下面的代码能够进行过滤,但每次过滤完成时过滤值都会丢失(变为空白)。

I have [BindProperties] and I don;t understand why it works for the UserList (the table data) and not for the filter criteria.我有 [BindProperties] 但我不明白为什么它适用于 UserList(表数据)而不适用于过滤条件。

I tried to reassign the values but it;s not working.我试图重新分配值,但它不起作用。 Does anyone has an idea how to solve this?有谁知道如何解决这个问题? Any pointer to the underlying reason and documentation page would be appreciated.任何指向潜在原因和文档页面的指针都将不胜感激。 I have looked at https://www.learnrazorpages.com/razor-pages/tempdata but I'm not sure it's the way to go.我看过https://www.learnrazorpages.com/razor-pages/tempdata但我不确定这是通往 go 的路。

Also do not hesitate to give feedback on the code itself as it's literally my first ASP.Net project.也不要犹豫,就代码本身提供反馈,因为它实际上是我的第一个 ASP.Net 项目。

UserList.cshtml用户列表.cshtml

@model Web.Areas.Admin.Pages.UserListModel
@{
    ViewData["Title"] = "User list";
}
<h1>User list</h1>

<div>
    <form method="post">
        <label asp-for="UserNameFilter"></label>
        <input type="text" name="UserNameFilter" /> @*This input criteria reset when I click on filter*@
        <input type="submit" value="Filter" />
    </form>
</div>

<table class="table">
    <thead>
        <tr>
            <th>Id</th>
            <th>User Name</th>
            <th>Email</th>
            <th>Password</th>
            <th>Actions</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var user in Model.UserInfos)
        {
        <tr>
            <td>@user.Id</td>
            <td>@user.UserName</td>
            <td>@user.Email</td>
            <td>
                <a asp-page="/UserUpdate" asp-route-id="@user.Id">Edit</a> |
                <a asp-page="/Details" asp-route-id="@user.Id">Details</a> |
                <a asp-page="/UserDelete" asp-route-id="@user.Id">Delete</a>
            </td>
        </tr>
        }
    </tbody>
</table>

UserList.cshtml.cs用户列表.cshtml.cs

using System.Collections.Generic;
using System.Linq;
using Entities;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Services;

namespace Web.Areas.Admin.Pages
{
    [Authorize(Roles = "ADMIN")]
    [BindProperties]
    public class UserListModel : PageModel
    {
        public string UserNameFilter { get; set; }
        public List<UserInfo> UserInfos { get; set; }
        private readonly IUserService _userService;

        public UserListModel(IUserService userService)
        {
            _userService = userService;
        }

        public void OnGet()
        {
            UserInfos = _userService.GetUserInfoList(new UserInfoFilter()).ToList();
        }

        public void OnPost()
        {
            var userInfoFilter = new UserInfoFilter
            {
                UserName = UserNameFilter
            };
            UserInfos = _userService.GetUserInfoList(userInfoFilter).ToList();
        }
    }
}

So that was a basic answer... you need to assign the value of the input with the model...所以这是一个基本的答案......你需要用 model 分配输入的值......

<input type="text" name="UserNameFilter" value="@Model.UserNameFilter"/> 

I'm leaving the post and answer here for other noob like me in future.我要离开这个职位并在这里回答未来像我这样的其他菜鸟。 Keep coding..继续打码。。

The above code works.上面的代码有效。 If someone else is looking for it, I want to add my scenario here.如果其他人正在寻找它,我想在这里添加我的场景。

I have two screens.我有两个屏幕。 One is sending an ID through asp-route-CurrentPOID一种是通过 asp-route-CurrentPOID 发送 ID

once on the other page, I was able to get it一旦在另一页上,我就能得到它

[BindProeprty(SupportsGet = true)]
public int POID {get;set;}


public void onGet(int CurrentPOID)
{
   CurrentPOID = whatever was selected on the other page
   // so I want to assign route value to this pages POID
   CurrentPOID = POID;  //works (only for onGet)

   _context.BLLMETHOD(currentPOID)  //works
   OR
   _context.BLLMETHOD(POID)  //works
}

However, I was not getting POID once I did onPost POID was null但是,一旦我做了 onPost POID 是 null,我就没有得到 POID

to solve this, I used exactly what the author of this post did为了解决这个问题,我完全使用了这篇文章的作者所做的

on front在前面

<input type="hidden" name="POID" value="@Model.POID"/> 

This solved my issue on Post, where POID was null.这解决了我在 Post 上的问题,其中 POID 是 null。

I am new to razor pages, so please pardon the inefficiency. razor页面我是新手,效率低下还请见谅。

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

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