获取 TABalance 中最后添加的值,并在页面加载时自动将其显示在 TSBalance 输入中

Get the last added value in TABalance and show it in TSBalance input automatically when the page loaded

在我的 html 代码下方

                <div class="border backgroundWhite border-info">
                    <div class="row">
                        <div class="col-6">
                            <div class="form-group row">
                                <div class="col-4">
                                    <label asp-for="Shift.TSBalance"></label>
                                </div>
                                <div class="col-6">
                                    <input asp-for="Shift.TSBalance" class="form-control" id="sbalance" readonly value="@Model.V" onkeyup="sum()" type="text" />
                                </div>
                            </div>
                            <div class="form-group row">
                                <div class="col-4">
                                    <label asp-for="Shift.TSupply"></label>
                                </div>
                                <div class="col-6">
                                    <input asp-for="Shift.TSupply" class="form-control" id="supply" value="0" onkeyup="sum()" type="text"/>
                                </div>
                                <span asp-validation-for="Shift.TSupply" class="text-danger"></span>
                            </div>
                            <div class="form-group row">
                                <div class="col-4">
                                    <label asp-for="Shift.TTotal"></label>
                                </div>
                                <div class="col-6">
                                    <input asp-for="Shift.TTotal" class="form-control" readonly id="trbalance" value="0" onkeyup="sum()" type="text"/>
                                </div>
                                <span asp-validation-for="Shift.TTotal" class="text-danger"></span>
                            </div>
                        </div>
                        <div class="col-6">
                            <div class="form-group row">
                                <div class="col-7">
                                    <label asp-for="Shift.TCBalance"></label>
                                </div>
                                <div class="col-5">
                                    <input asp-for="Shift.TCBalance" class="form-control" id="calcebalance" value="0" onkeyup="sum()" type="text" readonly/>
                                </div>
                            </div>
                            <div class="form-group row">
                                <div class="col-7">
                                    <label asp-for="Shift.TABalance"></label>
                                </div>
                                <div class="col-5">
                                    <input asp-for="Shift.TABalance" class="form-control" id="actualebalance" value="0" onkeyup="sum()" type="text" />
                                </div>
                            </div>
                            <div class="form-group row">
                                <div class="col-7">
                                    <label asp-for="Shift.TDifferance" class="text-info"></label>
                                </div>
                                <div class="col-5">
                                    <input asp-for="Shift.TDifferance" class="form-control" style="background-color:blue;color:white;font-size:larger" id="differance" value="0" onkeyup="sum()" type="text" readonly />
                                </div>
                            </div>
                        </div>
                     </div>
                      
                    
                </div>

            </div>
        </div>

我的页面模型

namespace Elwady.Pages.ShiftClosing
{
    public class CreateModel : PageModel
    {
        private readonly ApplicationDbContext _db;
        public SelectList Exlist { get; set; }
        [BindProperty]
        public Shift Shift { get; set; }
        public CreateModel(ApplicationDbContext db)
        {
            _db = db;
        }
        public IActionResult OnGet()
        {
            this.Exlist = new SelectList(_db.ExpensesList, "Id", "ExName");
            return Page();
        }
        public async Task<IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            };
            _db.Shift.Add(Shift);
            await _db.SaveChangesAsync();
            return RedirectToPage("../Index");
        }
    }
}

我的数据table

public class Shift
    {
        public int Id { get; set; }
        public int TSBalance { get; set; }
        public int TSupply { get; set; }
        public int TTotal { get; set; }
        public int TCBalance { get; set; }
        public int TABalance { get; set; }
        public int TDifferance { get; set; }

    }

我试图在 Get Handler 的变量中获取这个值,但我无法将它分配给页面 UI

如果我需要在提交到特定打印机后打印此表格,我该怎么做?

我是新来的Asp.Net

<input asp-for="Shift.TABalance" class="form-control" id="actualebalance" value="0" onkeyup="sum()" type="text" />

首先请检查输入元素,由于你添加了value属性(value="0"),在这种情况下,即使页面模型Shift包含数据,它也会显示0,因为value="0" 属性将覆盖 asp-for="Shift.TABalance".

因此尝试从输入元素中删除 value="0" 属性。

然后,要获取 TABalance 输入的值并将其分配给 TSBalance,您可以使用 onInput 事件。代码如下:

<div class="col-6">
    <input asp-for="Shift.TSBalance" class="form-control" id="sbalance" readonly value="@Model.Shift.TSBalance" onkeyup="sum()" type="text" />
</div>
<div class="col-5">
    <input asp-for="Shift.TABalance" class="form-control" id="actualebalance" onkeyup="sum()" oninput="TABalanceInput()" type="text" />
</div>

@section Scripts{
    <script>
    function sum(){

    }
    //update sbalance's value when update the actualebalance's value
    function TABalanceInput(){
          var actualebalance = document.getElementById("actualebalance");
          var s = actualebalance.value;

          var sbalance = document.getElementById("sbalance");
          sbalance.value = s;         
    }

    //after page loaded, trigger the TABalanceInput function and update the actualebalance's value
    $(document).ready(function(){
        TABalanceInput();
    });
    </script>
}

结果是这样的:页面加载后,会触发TABalanceInput函数更新值。并且如果修改TABalance的值,它也会更新TSBalance的值。

更新:

首先,这是上面示例中的 OnGet 方法:

    public IActionResult OnGet()
    {
        //this.Exlist = new SelectList(_db.ExpensesList, "Id", "ExName");

        this.Exlist = new SelectList(new List<SelectListItem>()
        {
            new SelectListItem(){ Value="1", Text="A"},
             new SelectListItem(){ Value="2", Text="B"},
              new SelectListItem(){ Value="3", Text="C"},
        });
        // create a new shift, and display them in the view.
        Shift = new Shift()
        {
            Id = 0,
            TABalance = 2,
            TCBalance = 0,
            TDifferance = 0,
            TSBalance = 0,
            TSupply = 0,
            TTotal = 0
        };

        return Page();
    }

i need the last value added to databse in TABalance column retrieved on TSBalance input box

在之前的回复中,我创建了一个新的 Shift 来渲染数据。如果要获取最后添加的数据,需要对shift数据进行软化,获取最后添加的item,然后return查看页面。参考以下代码:

    Shift = _db.Shifts.OrderByDescending(c => c.Id).First();

那么,结果是这样的: