vsc*_*der 5 razor .net-core asp.net-core
使用 Visual Studio 2017、.Net Core 2.1、网站
有了这个模型
public class TestObj
{
[DataType(DataType.Text)]
public string str1 { get; set; }
[DataType(DataType.Text)]
public int int1 { get; set; }
public List<TestObj> TestList { get; set; }
public TestObj() { }
public TestObj(string i_str, int i_int)
{
this.str1 = i_str;
this.int1 = i_int;
}
}
Run Code Online (Sandbox Code Playgroud)
还有这个控制器:
[HttpGet]
public IActionResult Test()
{
TestObj model = new TestObj();
model.TestList = new List<TestObj>();
model.TestList.Add(new TestObj("test1", 1));
model.TestList.Add(new TestObj("test2", 2));
return View(model);
}
Run Code Online (Sandbox Code Playgroud)
这是cshtml:
<form asp-action="Test" asp-controller="Home">
<ul>
@for (int i = 0; i < Model.TestList.Count; i++)
{
<li><label asp-for="@Model.TestList[i].str1" /></li>
<li><label asp-for="@Model.TestList[i].int1" /></li>
<li><input asp-for="@Model.TestList[i].str1" /></li>
<li><input asp-for="@Model.TestList[i].int1" /></li>
}
</ul>
Run Code Online (Sandbox Code Playgroud)
我得到了输入框,但没有得到标签。我已经搜索了网络和 Stack Overflow,但无法找出为什么输入有效时标签会失败。任何帮助,将不胜感激。
小智 4
我在这个问题上卡住了一段时间。label标签必须由另一个标签封闭,而不是在同一标签内。否则它将无法正常工作。
这是行不通的:
<label asp-for="Date" class="col-form-label col-sm-3" />
Run Code Online (Sandbox Code Playgroud)
这会:
<label asp-for="Date" class="col-form-label col-sm-3"></label>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2129 次 |
| 最近记录: |