Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.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
C# 如何使用asp for标记绑定数组?_C#_Asp.net Core_Asp.net Core Mvc - Fatal编程技术网

C# 如何使用asp for标记绑定数组?

C# 如何使用asp for标记绑定数组?,c#,asp.net-core,asp.net-core-mvc,C#,Asp.net Core,Asp.net Core Mvc,我想问,如何在Asp.NET Core MVC中绑定阵列 <input type="text" asp-for="Requests[@index].Name" /> 它应该如何工作?提交包含这些输入的表单后,MVC应自动在ModelView中创建并映射列表。这就是它在ASP.NET MVC 4中的工作方式。您需要使用可编译的ASP for的有效表达式,基本上,如果index是在for循环中使用的变量,那么您将编写 完整示例(我使用了I作为循环变量,而不是index): @model

我想问,如何在Asp.NET Core MVC中绑定阵列

<input type="text" asp-for="Requests[@index].Name" />

它应该如何工作?提交包含这些输入的表单后,MVC应自动在ModelView中创建并映射列表。这就是它在ASP.NET MVC 4中的工作方式。

您需要使用可编译的
ASP for
的有效表达式,基本上,如果
index
是在for循环中使用的变量,那么您将编写

完整示例(我使用了
I
作为循环变量,而不是
index
):

@model MyProject.TodoItemList
    @对于(int i=0;i @*或者旧的html助手,如果您愿意的话 @Html.DisplayFor(model=>model.Requests[i].Name) *@ }

有关详细信息,请检查中的表达式名称和集合,如果要使用单独的局部视图绑定列表

首先,创建返回列表的操作

public async Task<ActionResult> UpdateTable([FromForm]Filter filter)
{
    if (ModelState.IsValid)
    {           
        List<Model> model = new List<Model>();
        ViewData.TemplateInfo.HtmlFieldPrefix = string.Format("Requests");
        return PartialView("_PartialViewOfModel", model);
    }
    return BadRequest(ModelState);
}
public异步任务UpdateTable([FromForm]Filter)
{
if(ModelState.IsValid)
{           
列表模型=新列表();
ViewData.TemplateInfo.HtmlFieldPrefix=string.Format(“请求”);
返回PartialView(“PartialViewOfModel”,model);
}
返回请求(ModelState);
}
这段代码将出现在你的局部视图中

@model List<Model>
@for (int i = 0; i < Model.Count; i++)
{
    <input type="text" class="form-control form-control-sm" asp-for="@Model[i].Name" />
}
@型号列表
@for(int i=0;i
您能进一步解释一下您想要实现的目标吗?我添加了更多描述。谢谢!这个有效!我用的是foreach,但没用。
@model MyProject.TodoItemList

<ul>
@for (int i = 0; i < Model.Requests.Count; i++)
{
    <li>                
        <label asp-for="@Model.Requests[i].Name"></label>
        <input asp-for="@Model.Requests[i].Name"/>
        @* Or the old html helpers if you prefer
           @Html.DisplayFor(model => model.Requests[i].Name)
        *@                
    </li>
}
</ul>
public async Task<ActionResult> UpdateTable([FromForm]Filter filter)
{
    if (ModelState.IsValid)
    {           
        List<Model> model = new List<Model>();
        ViewData.TemplateInfo.HtmlFieldPrefix = string.Format("Requests");
        return PartialView("_PartialViewOfModel", model);
    }
    return BadRequest(ModelState);
}
@model List<Model>
@for (int i = 0; i < Model.Count; i++)
{
    <input type="text" class="form-control form-control-sm" asp-for="@Model[i].Name" />
}