ASP.NET mvc使用IEnumerable模型和输入标记帮助器查看

时间:2016-09-11 22:40:02

标签: razor asp.net-core asp.net-core-mvc tag-helpers

在此official ASP.NET Core tutorial中,我可以使用Input Tag Helper,如下所示。但由于known model binding issue of form elements in a foreach loop,我想改用for loop问题:如果我在以下@foreach (var item in Model)中将@for (int i=0; i < Model.Count(); i++)替换为Viewasp-for中的<input asp-for="???" />是什么?出于某种原因,智能感知不能识别,例如Model [i] .BlogId或@Model [i] .BlogId

@model IEnumerable<EFGetStarted.AspNetCore.NewDb.Models.Blog>

@{
    ViewBag.Title = "Blogs";
}

<h2>Blogs</h2>

<p>
    <a asp-controller="Blogs" asp-action="Create">Create New</a>
</p>

<table class="table">
    <tr>
        <th>Id</th>
        <th>Url</th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                <input asp-for="@item.BlogId" />
            </td>
            <td>
                <input asp-for="@item.Url" />
            </td>
        </tr>
    }
</table>

2 个答案:

答案 0 :(得分:0)

    @model IEnumerable<EFGetStarted.AspNetCore.NewDb.Models.Blog>

    @{
        ViewBag.Title = "Blogs";
    }

    <h2>Blogs</h2>

    <p>
        <a asp-controller="Blogs" asp-action="Create">Create New</a>
    </p>

    <table class="table">
        <tr>
            <th>Id</th>
            <th>Url</th>
        </tr>
    @for (int item = 0; item < Model.Count(); item++)
        {
           <tr>
              <td>
                <input name="@Model.ToList()[item].BlogId" />
              </td>
              <td>
                <input name="@Model.ToList()[item].Url" />
              </td>
          </tr>
       }
    </table>

答案 1 :(得分:0)

我遇到了同样的问题并以这种方式解决了这个问题:

export default class MyPageRecentsContainer extends Component { 
 state = { 
  recents: [], 
  page: 1, 
  numberOfPages: 1, 
} 

componentWillMount = () => { 
  this.getRecents(1) 
}  
getRecents = page => { 
 MyPageActions.getRecents({ 
   page: page, 
   pageSize: 6, 
 }) 
   .then(res => { 
     this.setState({ 
       recents: res.body, 
       ...calculatePagination(res.header), 
     }) 
   }) 
   .catch(err => { 
     alert(err.message) 
   }) 
} 


render() { 
 const { recents, page, numberOfPages, totalCount } = this.state 


 return ( 
   <MyPageRecentsComponent 
     recents={recents} 
     page={page} 
     numberOfPages={numberOfPages} 
     totalCount={totalCount} 
     getRecents={this.getRecents} 
   /> 
 ) 
} 

重要的部分是使用import { Pagination } from 'react-bootstrap' export default class MyPageRecentsComponent extends Component { render() { const { recents, page, numberOfPages, totalCount, getRecents, } = this.props return ( ... {totalCount > 6 ? ( <div className="section__pagination text-center"> <Pagination prev next maxButtons={5} items={numberOfPages} activePage={page} onSelect={newPage => getRecents(newPage)} /> </div> ) : null} ) } } 循环,并通过@for (int i = 0; i < Model.Translations.Count; i++) { @Html.HiddenFor(m => m.Translations[i].LanguageId) <form-group-text asp-for="@Model.Translations[i].Content"/> } 访问for

那就是说,如果您的模型本身应该是asp-for,您应该重新考虑。而是为它创建一个属性。此外,您需要确保您的收藏品可通过索引访问。您可以将属性设为@Model。当您为其分配IEnumerable时,可以先调用IList