c# - 如何在 ASP.NET Core RazorPages 中绑定复杂列表
问题描述
我是 ASP.NET Core Razor 页面的新手。我尝试通过 POST 从页面中检索 List<>。如果我绑定原始数据类型,我没有遇到任何问题。但是,如果我想将数据从我的页面传递到服务器,其中包含一个列表,我遇到了麻烦。我能够将数据从服务器传递到客户端,但不能返回。
这是我的代码的摘录:
剃刀页面:
<form method="post">
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Positions[0].Number)
</th>
<th>
@Html.DisplayNameFor(model => model.Positions[0].IsSelected)
</th>
</tr>
</thead>
<tbody>
@if (!(Model.Positions is null))
{
@foreach (var item in Model.Positions)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Number)
</td>
<td>
@Html.CheckBoxFor(modelItem => item.IsSelected)
</td>
</tr>
}
}
</tbody>
</table>
<input type="submit" />
后端 C# 文件:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using Project.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
namespace Project.Pages
{
public class TestModel : PageModel
{
private readonly DBContext _context;
[FromRoute]
public int? Id { get; set; }
public List<SelectListItem> CustomerOrder { get; set; } = new List<SelectListItem>();
[BindProperty]
public string SelectedNumber { get; set; }
[BindProperty]
public List<Position> Positions { get; set; }
public TestModel(Project.Models.DBContext context)
{
_context = context;
}
public void OnGet()
{
if (!(Id is null))
{
_context.CustomerOrder.Select(co => co.OrderNumber).Distinct().ToList().ForEach(y => CustomerOrder.Add(new SelectListItem(text: y, value: y)));
}
}
public async Task OnPostAsync()
{
if (!(SelectedNumber is null))
{
string s = $@"
select * from Table1 xyz where xyz.Column1 in (
SELECT distinct Column1
FROM Table1
where value = '" + SelectedNumber + "') and xyz.name = 'SLLZ'";
var res = await _context.Table1.FromSql(s).Select(x => x.ValueDescription).Distinct().OrderBy(x => x).ToListAsync();
Positions = new List<Position>();
foreach (var item in res)
{
Positions.Add(new Position { Number = item });
}
}
_context.CustomerOrder.Select(co => co.OrderNumber).Distinct().ToList().ForEach(y => CustomerOrder.Add(new SelectListItem(text: y, value: y)));
}
}
public class Position
{
public string Number { get; set; }
public bool IsSelected { get; set; }
}
}
如果我在 OnPost 方法的开头设置断点,我希望列表中填充与复选框中用户输入相关的 IsSelected-Property,但事实并非如此。
解决方案
复杂对象绑定的关键是确保将方括号中的顺序索引添加到表单字段的名称属性中,例如[0].IsSelected
或Positions[0].IsSelected
在您的情况下。您可以很容易地使用for
循环和标签助手输出正确的 HTML。
您可以在此处阅读有关主体的更多信息:https ://www.learnrazorpages.com/razor-pages/model-binding#binding-complex-collections 。然后你应该能够将它应用到你的应用程序中。
推荐阅读
- java - 如何在android中调整捕获的图像大小
- sql - 获取第一个合同的类型字段
- c++ - while 循环内的 while 循环的时间复杂度是多少?
- ios - swift4 Firebase UI 身份验证不适用于 Twitter
- regex - PowerShell :: 文件中的正则表达式搜索
- javascript - 在函数中调用函数不起作用,未捕获的 TypeError
- redis - Jedis Vs Spring Data Redis Vs Reddison - 当您有一些复杂的查询逻辑时,更喜欢哪一个?
- xcode - 我可以在 Xcode 中拥有多个资产目录吗?
- c - 通过函数更改时,主数组中的值不更新
- sql-server - SSRS 表达式的 SQL 语法替换