我正在尝试做一些我认为相对简单但无法使其发挥作用的事情,并且会对某些指针感到满意。
我有一个带有文本框列表的表单...让我们说,我想找出“你最喜欢的海盗”,并让你列出页面上的所有十个,并注明为什么他们这些是你最喜欢的。
所以在我看来,我有:
for (int i =1; i <11; i++)
{%>
<%=Html.TextBoxFor(x => x.Pirate + i, new { size = 30, maxlength = 200 })%>
<%=Html.TextAreaFor(x => x.PirateReason + i, new { cols = 42, rows = 2 })%>
<%
}%>
但是如何在我的模型中设置它?
很抱歉,如果不具体。
在我的模型中,我只想存储海盗列表,在我目前正在进行的示例中,只有10名海盗,所以我可以这样做,如果我不得不
public string Pirate1 { get; set; }
public string Pirate2 { get; set; }
public string Pirate3 { get; set; }
public string Pirate4 { get; set; }
public string Pirate5 { get; set; }
public string Pirate6 { get; set; }
public string Pirate7 { get; set; }
public string Pirate8 { get; set; }
public string Pirate9 { get; set; }
public string Pirate10 { get; set; }
但那太可怕了,如果我想知道你最喜欢的100名海盗怎么办?
我想将盗版存储在模型中,以便我可以将它们弹出数据库或作为电子邮件发送......
非常感谢你的建议..
答案 0 :(得分:1)
型号:
public class Pirate
{
public int Id { get; set; }
public string PirateReason { get; set; }
}
控制器操作:
public ActionResult Index()
{
var model = Enumerable
.Range(1, 11)
.Select(i => new Pirate {
Id = i,
PirateReason = string.Format("reason {0}", i)
});
return View(model);
}
IEnumerable<Pirate>
的强类型视图:
<%= Html.EditorForModel() %>
编辑模板(~Views/Shared/EditorTemplates/Pirate.ascx
):
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<SomeNs.Pirate>" %>
<%= Html.TextBoxFor(x => x.Id, new { size = 30, maxlength = 200 }) %>
<%= Html.TextAreaFor(x => x.PirateReason, new { cols = 42, rows = 2 }) %>