Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Razor 如何在for循环中设置asp for_Razor_Asp.net Core_Tag Helpers_Asp.net Core Tag Helpers - Fatal编程技术网

Razor 如何在for循环中设置asp for

Razor 如何在for循环中设置asp for,razor,asp.net-core,tag-helpers,asp.net-core-tag-helpers,Razor,Asp.net Core,Tag Helpers,Asp.net Core Tag Helpers,我有一个for循环来生成表单上的字段 如何根据当前索引为设置asp的值 不起作用。您可以使用etherforeach或for将模型与这样的索引绑定: @page @model IndexModel @{ ViewData["Title"] = "Home page"; int i = 0; } <form method="post"> @foreach (var item in Model.Items) { <input asp

我有一个for循环来生成表单上的字段

如何根据当前索引为设置asp的值


不起作用。

您可以使用ether
foreach
for
将模型与这样的索引绑定:

@page
@model IndexModel
@{
    ViewData["Title"] = "Home page";
    int i = 0;
}
<form method="post">
    @foreach (var item in Model.Items)
    {

        <input asp-for="Items[i]" />
        i++;
    }

    @for (int j = 0; j < Model.Items.Count; j++)
    {

        <input asp-for="Items[j]" />
        j++;
    }
    <button type="submit">Submit</button>
</form>
结果如下:

提交表单后,由于html
name
属性,输入字段中的值将出现在字符串列表中


您必须了解Razor使用标记帮助器
asp for
在字段上生成各种html属性,例如name/id/data-*等。。。您需要的主要内容是
name
属性来绑定值。

Eh什么?不清楚你在问什么。在asp中为您传递模型属性。你把它和HTML的
混淆了吗?!当您在数组中循环时,您已经有了对该项的引用。i、
foreach(模型中的var项){…}
然后您有一个对
item
的引用,因此您可以在
asp中为
使用它,比如
asp for=“item.Value”
asp for=“item.Name”
这取决于您在模型类上的属性是如何命名的,您所说的“不工作”是什么意思?
@(“值”+i)
的输出是什么?你期望的是什么?有错误消息吗?你不能。为
asp传递的值必须是
ModelExpression
。它不能接受表示属性的通用字符串值。然而,FWIW拥有诸如
Value1
Value2
Value3
等属性,这通常是错误的。使用集合类型:即
。然后,您在这里尝试做的是微不足道的,因为您只是索引该集合,它可以表示为
ModelExpression
.Tseng>>我的模型有几个属性Value1…Value31。根据某些条件,我需要提供输入字段或隐藏字段。因此for循环更像是
for(int i=1;i>>我得到一个错误invalidooperationexception:模板只能用于字段访问、属性访问、单个…这与@ChritPratt提出的想法相同。我一直在寻找类似的东西:
@for(inti=1;我错过了列表并将其更改为“@model List”。最终,它运行良好。
public class IndexModel : PageModel
{
    [BindProperty]
    public List<string> Items { get; set; }

    public void OnGet()
    {
        Items = new List<string> { "one", "two", "three" };
    }

    public void OnPost(List<string> items)
    {

    }
}
[Route("[controller]")]
public class TestController : Controller
{

    [HttpGet("[action]")]
    public IActionResult Test()
    {
        return View(new List<string> { "one", "two", "three" });
    }

    [HttpPost("[action]")]
    public IActionResult Test(List<string> Words)
    {
        return Ok();
    }
}
@model List<string>

@{
    int i = 0;
}

<form method="post">

    @foreach (var item in Model)
    {
        <label>@item  - @i</label>
        <input name="Words[@i]" value="@item" />

        i++;
    }
    <button type="submit">Submit</button>
</form>