我尝试在我的代码中使用以下输入标签帮助程序示例,但它不起作用。 https://docs.microsoft.com/de-de/aspnet/core/mvc/views/working-with-forms?view=aspnetcore-2.2 https://www.learnrazorpages.com/razor-pages/tag-helpers/input-tag-helper
我想使用对象列表显示在表中并从输入中读取值。显示数据有效,但不读取输入。
cshtml
@page
@{
var days2 = @Model.days;
}
<form class="form-horizontal" method="post">
<button asp-page-handler="Go" type="submit" class="btn btn-default">Go</button>
<table class="table">
<thead>
<tr>
<th>@Html.DisplayNameFor(m => m.days[0].Name)</th>
<th>@Html.DisplayNameFor(m => m.days[0].Beginn)</th>
........
</tr>
</thead>
<tbody>
@for (int i =0; i< days2.Count; i++)
{
<tr>
<td><input asp-for="@days2[i].Name" class="form-control" value=Tag.Name /></td>
<td><input asp-for="@days2[i].Beginn" class="form-control" value=Tag.Beginn /></td>
.....
</tr>
}
</tbody>
</table>
</form>
cshtml.cs
[BindProperty]
public List<day> days { get; set; }
public void OnGet()
{
days=getdaysvalue();
}
public void OnPostGo()
{
for (int i = 0; i < Tage.Count; i++)
{
days[i].Name = days2[i].Name; //days2 is in the actuell context not given
days[i].Beginn = days2[i].Beginn;
}
days2在actuell上下文中未提供
答案 0 :(得分:0)
您在视图中有几个具有相同名称和ID的属性,最后它只读取其中一个属性的值。
您可以尝试
@for (int i =0; i< days2.Count; i++)
{
<tr>
<td><input id="Name_@i" type="text" name="Name" class="form-control" value="@Tag.Name" /></td>
<td><input id="Beginn_@i" type="text" name="Beginn" class="form-control" value="@Tag.Beginn" /></td>
.....
</tr>
}
和在RazorPage中
public void OnPostGo(List<string> Name,List<string> Beginn)
{
for (int i = 0; i < Tage.Count; i++)
{
days[i].Name = Name[i];
days[i].Beginn = Beginn[i];
}
}
答案 1 :(得分:0)
对于Razor Page,它有助于将模型与[BindProperty] public List<day> days { get; set; }
绑定,您应该尝试通过days
而不是view
中的变量来访问提交模型。
尝试:
PageModel
public class TestModel : PageModel
{
[BindProperty]
public List<day> days { get; set; }
public void OnGet()
{
days = new List<day> {
new day{ Name = "D1", Beginn = "B1"},
new day{ Name = "D2", Beginn = "B2"},
new day{ Name = "D3", Beginn = "B3"},
};
}
public void OnPostGo()
{
var result = days;
}
}
查看
@page
@model CoreRazor.Pages.Books.TestModel
@{
ViewData["Title"] = "Test";
}
<h1>Test</h1>
<form class="form-horizontal" method="post">
<button asp-page-handler="Go" type="submit" class="btn btn-default">Go</button>
<table class="table">
<thead>
<tr>
<th>@Html.DisplayNameFor(m => m.days[0].Name)</th>
<th>@Html.DisplayNameFor(m => m.days[0].Beginn)</th>
........
</tr>
</thead>
<tbody>
@for (int i = 0; i < Model.days.Count; i++)
{
<tr>
<td><input asp-for="@Model.days[i].Name" class="form-control" /></td>
<td><input asp-for="@Model.days[i].Beginn" class="form-control" /></td>
</tr>
}
</tbody>
</table>
</form>