C# 如果在foreach循环下动态生成Textarea不止一次,如何在Razor(blazor)组件中获取Textarea值
如果在foreach循环下动态生成Textarea超过一次,请任何人帮助从Razor组件获取Textarea值 我尝试了C# 如果在foreach循环下动态生成Textarea不止一次,如何在Razor(blazor)组件中获取Textarea值,c#,html,asp.net-core,blazor,C#,Html,Asp.net Core,Blazor,如果在foreach循环下动态生成Textarea超过一次,请任何人帮助从Razor组件获取Textarea值 我尝试了@bind Value和Value,但仍然无法单独获取所有textarears值 Razor component code <EditForm Model="@userans" OnValidSubmit="@oninput"> <DataAnnotationsValidator /> <h3>Test </h3>
@bind Value和Value
,但仍然无法单独获取所有textarears值
Razor component code
<EditForm Model="@userans" OnValidSubmit="@oninput">
<DataAnnotationsValidator />
<h3>Test </h3>
@if (Questiontable == null)
{
<p><em>Loading...</em></p>
}
else
{
<table class="table">
<thead>
<tr>
</tr>
</thead>
<tbody>
@foreach (var item in Questiontable[0].Questiondata)
{
<tr>
<td>@item.Question</td>
</tr>
<tr>
<th>
**<textarea id="TextArea1" rows="2" cols="20" @bind="@userans.answers"></textarea>**
</th>
</tr>
}
</tbody>
</table>
<div align="center">
<button class="btn btn-primary" type="submit" @onclick="btnnextclick">Next</button>
</div>
}
</EditForm>
Razor组件代码
试验
@if(Questiontable==null)
{
加载
}
其他的
{
@foreach(Questiontable[0]中的变量项。Questiondata)
{
@项目.问题
****
}
下一个
}
Razor组件正在加载来自数据库的数据,并生成与数据库记录一样多的Textarea,但只要im更新Textarea,我就需要获得不同Textarea中的所有可用值 任何时候,只要您想根据动态输入量获取输入,就应该实例化一个数组或列表。 然后,对于生成标记的循环中的每个迭代,向列表中添加一个实例,然后将输入绑定到数组中输入实例的索引值 我以前没有使用过blazor,所以请不要引用我的语法,但这应该是您需要的总体概念:
@{
List<string> myInputs = new List<string>();
}
@for (int i = 0; i < Questiontable[0].Questiondata.length; i++){
myInputs.Add(string.Empty);
<th>
<textarea id="TextArea" rows="2" cols="20" @bind="@myInputs[i]"></textarea>
</th>
}
@{
List myInputs=新列表();
}
@对于(int i=0;i
希望这对你有帮助 问题是您的每个文本区域都绑定到相同的
用户。答案
你可以做一些我刚刚测试过的事情:
@page "/test-loop"
<h3>TestLoop</h3>
@foreach (var test in TestList)
{
<div>Id: @test.Id</div>
<textarea @bind="test.TextAreaValue">
</textarea>
}
@* To show it's working: *@/
@foreach (var test in TestList)
{
<div>Id: @test.Id</div>
@test.TextAreaValue
}
<br />
@* Get The values on an event test *@
<button @onclick="ButtonClicked">
Test
</button>
@code {
List<TestObject> TestList = new List<TestObject>();
protected override void OnInitialized()
{
base.OnInitialized();
TestList.Add(new TestObject()
{
Id = 1
});
TestList.Add(new TestObject()
{
Id = 2
});
TestList.Add(new TestObject()
{
Id = 3
});
}
public void ButtonClicked()
{
//TestList has the values of each textarea in it
}
public class TestObject
{
public int Id { get; set; }
public string TextAreaValue { get; set; }
}
}
@page”/test循环
测试回路
@foreach(TestList中的var测试)
{
Id:@test.Id
}
@*要显示它正在工作,请执行以下操作:*@/
@foreach(TestList中的var测试)
{
Id:@test.Id
@test.TextAreaValue
}
@*获取事件测试的值*@
试验
@代码{
List TestList=新列表();
受保护的覆盖无效OnInitialized()
{
base.OnInitialized();
添加(新的TestObject()
{
Id=1
});
添加(新的TestObject()
{
Id=2
});
添加(新的TestObject()
{
Id=3
});
}
公共无效按钮单击()
{
//TestList中有每个textarea的值
}
公共类测试对象
{
公共int Id{get;set;}
公共字符串TextAreaValue{get;set;}
}
}
userans的类型是什么?回答,我认为它应该是一个列表,如果你将它设置为字符串,这就是你不能单独获取所有Textareas值的原因。@XingZou你是对的,userans。回答是类的属性,我现在使用了列表及其工作方式。谢谢@kyle,它工作了,唯一的问题是我使用了类而不是if List。现在我要把课堂改成列表。请您解释一下,为什么它不能与类一起工作?因为在这种情况下必需的字段验证器不能工作,对吗?如果您将它们都绑定到一个类,那么每个输入都绑定到同一个对象。如果将它们全部存储为类列表,则每个输入都绑定到单个类。