使用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;
}
}
和此控制器:
[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);
}
这是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>
我获得了输入框,但没有标签。我已经搜索了网络并堆叠溢出,但是找不到输入工作时标签失败的原因。任何帮助将不胜感激。
我被困在这个上。标签标签必须由另一个标签关闭,而不是在同一标签内。否则它将无法正常工作。
这将不是工作:
<label asp-for="Date" class="col-form-label col-sm-3" />
这将:
<label asp-for="Date" class="col-form-label col-sm-3"></label>
您必须为此进行几个更改
-
如Seantech所说,您必须使用
封闭</label>
<label>
标签 -
标签助手是从@addtaghelper *,microsoft.aspnetcore.mvc.taghelpers导入的它可以在viewimorts.cshtml中找到。确保该文件应以您的视图/页面的根级别可用文件夹。
最重要的是 - 您需要将此行添加到_viewimports.cshtml文件中。并且您需要确保,特定的_viewimports文件用于您的视图或页面文件夹。