为什么属性从视图到代码页面无效

发布于 2025-02-06 00:31:18 字数 1277 浏览 0 评论 0原文

我有这样的东西(简短的)支出

。cshtml.cs:

    public class Expenditures: PageModel {
        [BindProperty(SupportsGet = true)] public string PersonnelNo {get;set;}
        
        public async Task OnGetAsync() { //some code 
        }

        public async Task < IActionResult > OnPostAsync() {
            return Page();
          }
    }

and earvenditures.cshtml,

@page
@using DataAccessLibrary.Models
@model WorkInProgress.Pages.Expenditures
@{
ViewData["Title"] = "Expenditures";
}
<h1>Project Information</h1>
<form method="post">
   <table class="table table-hover">
      <thead class = "table-light">
         <tr>
            <th>@Model.PersonnelNo</th>
         </tr>
      </thead>
   </table>
   <div class="form-control">
      @Html.HiddenFor(model => model.PersonnelNo, Model.PersonnelNo)
      <input type="submit" value="Save" class="btn btn-primary"/>
   </div>
</form>

这是第一次起作用,当页面加载时(.cs文件中的personnelno具有值为值),

但是当我按提交按钮时,尝试重新加载同一页面,personnelno的值无效。

有没有办法将参数从视图回到.cs文件,以便可以重复使用?

我以为@html.hiddenfor(model =&gt; model.personnelno,model.personnelno)应该这样做,但不起作用

I have something like this (reduced for brevity)

Expenditures.cshtml.cs:

    public class Expenditures: PageModel {
        [BindProperty(SupportsGet = true)] public string PersonnelNo {get;set;}
        
        public async Task OnGetAsync() { //some code 
        }

        public async Task < IActionResult > OnPostAsync() {
            return Page();
          }
    }

and Expenditures.cshtml

@page
@using DataAccessLibrary.Models
@model WorkInProgress.Pages.Expenditures
@{
ViewData["Title"] = "Expenditures";
}
<h1>Project Information</h1>
<form method="post">
   <table class="table table-hover">
      <thead class = "table-light">
         <tr>
            <th>@Model.PersonnelNo</th>
         </tr>
      </thead>
   </table>
   <div class="form-control">
      @Html.HiddenFor(model => model.PersonnelNo, Model.PersonnelNo)
      <input type="submit" value="Save" class="btn btn-primary"/>
   </div>
</form>

this works the first time, when the page loads (PersonnelNo in the .cs file has a value)

however when I press the submit button and I try to reload the same page, the value for PersonnelNo is nulled.

Is there a way to pass the parameter from the view back to the .cs file so it can be reused?

I thought the @Html.HiddenFor(model => model.PersonnelNo, Model.PersonnelNo) should do this, but is not working

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我很坚强 2025-02-13 00:31:18

在演示下,我可以获得价值,您可以参考它。

第一种方法

将参数添加到处理程序方法。

public class IndexModel : PageModel
    {
        [BindProperty(SupportsGet = true)]       
        public string PersonnelNo { get; set; }

        public async Task OnGetAsync()
        {
            PersonnelNo = "1";
        }
    
        public async Task<IActionResult> OnPostAsync(string PersonnelNo)
        {
            return Page();
        }
    }

查看:

@page
@model IndexModel
@{
    ViewData["Title"] = "Home page";
}

<h1>Project Information</h1>
<form method="post">
   <table class="table table-hover">
      <thead class = "table-light">
         <tr>
            <th>@Model.PersonnelNo</th>
         </tr>
      </thead>
   </table>
   <div class="form-control">
      @Html.HiddenFor(model => model.PersonnelNo, Model.PersonnelNo)
      <input type="submit" value="Save" class="btn btn-primary"/>
   </div>
</form>

结果:

“在此处输入图像描述”

第二种方法

添加名称属性do do Model banding

添加此行&lt; input name =“ PersonnelNo” type =“ sideen”/&gt;之后@html.hiddenfor(model =&gt; model.personnelno,model.personnelno)

结果:

“

Below demo I can get the value, you can refer to it.

First way

Adding parameter to the handler method.

public class IndexModel : PageModel
    {
        [BindProperty(SupportsGet = true)]       
        public string PersonnelNo { get; set; }

        public async Task OnGetAsync()
        {
            PersonnelNo = "1";
        }
    
        public async Task<IActionResult> OnPostAsync(string PersonnelNo)
        {
            return Page();
        }
    }

View:

@page
@model IndexModel
@{
    ViewData["Title"] = "Home page";
}

<h1>Project Information</h1>
<form method="post">
   <table class="table table-hover">
      <thead class = "table-light">
         <tr>
            <th>@Model.PersonnelNo</th>
         </tr>
      </thead>
   </table>
   <div class="form-control">
      @Html.HiddenFor(model => model.PersonnelNo, Model.PersonnelNo)
      <input type="submit" value="Save" class="btn btn-primary"/>
   </div>
</form>

Result:

enter image description here

Second way

Add name attribute to do model binding

Add this line<input name="PersonnelNo" type="hidden" /> after @Html.HiddenFor(model => model.PersonnelNo, Model.PersonnelNo)

Result:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文