How can I bind an array with asp-for tag?

依然范特西╮ 提交于 2019-12-30 18:33:47

问题


I would like to ask, how can I bind an array in Asp.NET Core MVC ?

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

It was working very well in older versions of ASP MVC. This example shows "Internal server error".

"An error occurred during the compilation of a resource required to process this request. Please review the following specific error details and modify your source code appropriately."

ViewModel class example:

public class ViewModel
{
    public List<Model> Requests {get;set;}
}

Model class example:

public class Model
{
    public string Name {get;set;}
}

How it should work ? After you submit a form with these inputs, MVC should automaticly create and map the list in ModelView. That's how It works in ASP.NET MVC 4.


回答1:


You need to use a valid expression with asp-for that can be compiled, basically if index is a variable you are using in a for loop then you would write <input asp-for="@Model.Requests[index].Name" />

Full example (I've used i as the loop variable instead of index):

@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>

For more info, check Expression names and Collections in the docs




回答2:


If you want to bind list using separate partial view that time the flow will be like it.

First create your action which return the list.

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);
    }

And the below code will go in your partial view.

@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" />}


来源:https://stackoverflow.com/questions/41873171/how-can-i-bind-an-array-with-asp-for-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!