列表模型不返回数据

时间:2015-06-30 11:23:31

标签: c# asp.net-mvc

我有下面的代码,它在编辑模式下加载。但当我得到信息回到服务器时,它会返回任何行,任何想法为什么。该方法确实进入下面的post方法。这将是编辑模式下的更新方法。




  @model List< SubjectSuggestionModel>
 @using Telerik.Sitefinity.UI.MVC
 @using Telerik.Sitefinity.UI.MVC.Helpers
 < script type =“text / javascript”>
 $(document).ready(function(){
 $(“#grid”)。kendoGrid({
 sortable:true,
 pageable:true,
 dataSource:{pageSize :20}
});
});
 < /脚本>
 < div class =“container”>
 <字段集>
 < table id =“grid”>
 < THEAD>
 < TR>
 < th data-sortable =“false”>
主题ID
 < /第>
 < th data-sortable =“false”>
建议订单
 < /第>
 < th data-field =“QuestionText”>
推荐主题
 < /第>
 < /第>
 < / TR>
 < / THEAD>
 @using(Html.BeginFormSitefinity(“editSubjectSuggestions”,“editSubjectSuggestions”))
 {
 @ Html.ValidationSummary(真)
 < TBODY>
 @foreach(模型中的var i)
 {
 < TR>
 < td> @ Html.TextBoxFor(model => i.subject_id,new {@readonly =“readonly”})
 < / TD>
 < td> @ Html.TextBoxFor(model => i.suggestion_order)
 < / TD>
 < td> @ Html.TextBoxFor(model => i.suggested_subject_id)
 < / TD>
 < / TR>
 }
 < / tbody的>

 }
 < /表>
 < input type =“submit”value =“confirm”runat =“server”/>
 < /字段集>
 < / div>
  




动作方法




  [HttpPost ]
 public ActionResult editSubjectSuggestions(List< SubjectSuggestionModel> models)
 {
 return view(“subjectSuggestion”,prsubList.getSubjectSuggestionsList(
 Convert.ToInt32(Session [“subjectID”]。ToString())));
}
  



1 个答案:

答案 0 :(得分:2)

使用foreach循环生成重复的id属性(无效的html)和重复的name属性,而不使用无法绑定到集合的索引器。您必须使用for循环或自定义EditorTemplate作为类型。

使用for循环

<tbody>
  @for (int i = 0; i < Model.Count; i++)
  {
    <tr>
      <td>@Html.TextBoxFor(m => m[i].subject_id, new { @readonly = "readonly" })</td>
      <td>@Html.TextBoxFor(m => m[i].suggestion_order)</td>
      <td>@Html.TextBoxFor(m => m[i].suggested_subject_id)</td>
    </tr>
  }
</tbody>

使用EditorTemplate

/Views/Shared/EditorTemplates/SubjectSuggestionModel.cshtml

@model SubjectSuggestionModel
<tr>
  <td>@Html.TextBoxFor(m => m.subject_id, new { @readonly = "readonly" })</td>
  <td>@Html.TextBoxFor(m => m.suggestion_order)</td>
  <td>@Html.TextBoxFor(m => m.suggested_subject_id)</td>
</tr>

并在主视图中

<tbody>
  @Html.EditorFor(m => m)
</tbody>