Modelbinding名单 [英] Modelbinding lists

查看:136
本文介绍了Modelbinding名单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了像

public class Question {   
   public int Id { get;set; }
   public string Question { get;set; }
   public string Answer { get;set; } 
}

public ActionResult Questions() 
{   
  return View(GetQuestions()); 
}

public ActionResult SaveAnswers(List<Question> answers) 
{  
  ... 
}

视图>如下:

<% for (int i = 0; i < Model.Count; i++) { %>   
 <div>
  <%= Html.Hidden(i.ToString() + ".Id") %>
  <%= Model[i].Question %>
  <%= Html.TextBox(i.ToString() + ".Answer") %>
 </div> 
<% } %>

显然,这种观点是行不通的。我只是无法访问视图列表中。

Obviously this view doesn't work. I'm just not able access the list in the view.

这也是过时的文件,它似乎周围很多modelbinding列表的功能,其中在测试改变。

The documentation for this also is outdated, it seem a lot of the functionality around modelbinding lists where changed in the beta.

推荐答案

答案是不使用HTML佣工。

the answer is not to use the html helpers.

<% for (int i = 0; i < Model.Count; i++) { %> 
  <div>
     <input type="hidden" name="answers[<%= i %>].Id" id="answers_<%= i %>_Id" value="<%= Model[i].Id %>" />
     <input type="text" name="answers[<%= i %>].Answer" id="answers_<%= i %>_Answer" value="<%= Model[i].Answer %>" />
  </div> 
<% } %>

不是很pretty,但作品。重要的是,姓名和身份证需要是不同的。
名称允许有[,],但id是不是。

Not very pretty, but works. The important thing is that Name and Id need to be different. Name is allowed to have "[", "]" but id isn't.

这篇关于Modelbinding名单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆