ASP.NET MVC - 如何在模型为空时循环生成空输入框?
ASP.NET MVC - How to generate empty input boxes in loop when model is empty?
我正在使用 ASP.NET MVC 4 和 LINQ/SQL。我有一个 HTML 页面,其中包含一个表单,我想在其中呈现基于模型 Class 的 HTML 输入框。在我看来,我正在遍历模型并尝试为每个 table 行生成一个输入框,但由于模型为空,因此输入框不显示。该模型是空的,因为数据库中还没有数据,因为这是我想在我的表单中收集的数据。当模型中有数据时,数据库中的值应显示在表格中。
如果我的模型最初是空的,我该如何渲染空的输入框。
代码如下:
@for (int i = 0; i < Model.Count; i++)
{
<tr>
<td>
@Html.EditorFor(model => model[i].CustomerName, new { htmlAttributes = new { @class = "form-control" } })
</td>
</tr>
}
使用 For 循环和您需要的多个框,并在模型为 null 时进行验证,使用 For 循环
@{
if (Model == null)
{
int textboxnumber = 6;//change value on behind code or put a constant number
for(int i=0;i<textboxnumber;i++)
{
<input type="text" placeholder="textbox empty"/>
}
}
}
我正在使用 ASP.NET MVC 4 和 LINQ/SQL。我有一个 HTML 页面,其中包含一个表单,我想在其中呈现基于模型 Class 的 HTML 输入框。在我看来,我正在遍历模型并尝试为每个 table 行生成一个输入框,但由于模型为空,因此输入框不显示。该模型是空的,因为数据库中还没有数据,因为这是我想在我的表单中收集的数据。当模型中有数据时,数据库中的值应显示在表格中。
如果我的模型最初是空的,我该如何渲染空的输入框。
代码如下:
@for (int i = 0; i < Model.Count; i++)
{
<tr>
<td>
@Html.EditorFor(model => model[i].CustomerName, new { htmlAttributes = new { @class = "form-control" } })
</td>
</tr>
}
使用 For 循环和您需要的多个框,并在模型为 null 时进行验证,使用 For 循环
@{
if (Model == null)
{
int textboxnumber = 6;//change value on behind code or put a constant number
for(int i=0;i<textboxnumber;i++)
{
<input type="text" placeholder="textbox empty"/>
}
}
}