EDIT2
我創建了一個工作快速樣本項目。有一件事我不喜歡,那就是我無法繞過這個名單。我必須每次創建空白列表並從文本框中讀取所有值並將其保存在列表中,並將此更新列表提供給新視圖。接下來的一回事。但它的工作。
基本上是:
public ActionResult About() {
List<FormMetaData> formItems = GetFormItems();
return View(formItems);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult About(FormCollection form)
{
List<FormMetaData> formItems = GetFormItems();
//TryUpdateModel(formItems);
// update project members
foreach (var key in form.AllKeys) {
if (key.ToString().StartsWith("TextBox")) {
string field = (key.ToString().Replace("TextBox", ""));
if (!string.IsNullOrEmpty(form.Get(key.ToString()))) {
formItems.Find(delegate(FormMetaData t) { return t.Field == field; }).Value = form.Get(key.ToString());
}
else { }
// this.ProjectRepository.DeleteMemberFromProject(id, userId);
}
}
ModelState.AddModelError("test", "this is a test error");
if(ModelState.IsValid)
{
///
}
else
{
return View(formItems);
}
return View(formItems);
}
private List<FormMetaData> GetFormItems() {
List<FormMetaData> output = new List<FormMetaData>();
FormMetaData temp1 = new FormMetaData("TextBox",true,"temp1","displayText1");
FormMetaData temp2 = new FormMetaData("TextBox", true, "temp2", "displayText2");
output.Add(temp1);
output.Add(temp2);
return output;
}
,然後你有你的觀點:
<% using (Html.BeginForm()) {%>
<table>
<% foreach (var item in Model) {
if (!item.isActive) {
continue;
} %>
<tr>
<td>
<%=Html.Encode(item.DisplayValue)%>
</td>
<td>
<% if (item.FieldType == "TextBox") {%>
<%=Html.TextBox("TextBox"+item.Field, item.Value)%>
<%} if (item.FieldType == "CheckBox") {%>
<%=Html.CheckBox("Value")%>
<%}%>
</td>
<td>
</td>
</tr>
<%} %>
<p>
<input type="submit" value="submit" />
</p>
<% } %>
</table>
我已上傳的壓縮文件給你@http://www.bastijn.nl/zooi/dynamicSample.rar
編輯
我曾嘗試這個例子,它會去w與modelBinder合作。當我使用「FormCollection窗體」作爲POST創建方法的輸入時,我的文本框的值在提供的鍵下。因此,您必須使用自定義模型聯編程序或創建一個可與默認模型聯編程序配合使用的模型。
更具體一點。它出錯了,因爲在這種情況下,您的文本框正在更新列表內的對象中的屬性,這是型號通過的對象。通常情況下,您的文本框正在更新對象中的屬性,它也是也是您的型號,用於文本框的鍵(用於自動模型綁定)是您更新的屬性的名稱。
所以我想模型聯編程序不會將文本框中的值綁定到列表中的項目,因爲它只是不知道如何自動執行此操作。現在是3點17分,所以我現在要睡覺了,這個問題很有意思,明天我可能會完成答案。
原
<%=Html.TextBox(item.Field, null, new { tabindex = item.SortOrder })%>
這似乎與設置爲空值每次要生成您的形式。
嘗試初始化它們是這樣的:
<%=Html.TextBox(item.Field, **item.Value**, new { tabindex = item.SortOrder })%>
而在你的控制,當你檢查ModelState.isValid做這樣的事情:
if(ModelState.isValid){
//code when it works
}
else{
return View(formItems) // these should contain the just added values
}
這應該做的伎倆。
所以,在一個簡單的例子,你喜歡的東西:
public ActionResult Create()
{
List<FormMetadata> formItems = GetFormItems();
return View(formItems);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(List<FormMetadata> formItems)
{
if(ModelState.isValid)
{
MyUpdate()
save()
}
else
{
return view(formItems)
}
}
而且你的觀點:
<%
if (item.FieldType == "TextBox")
{%>
<%=Html.TextBox(item.Field, **item.Value**, new { tabindex = item.SortOrder })%>
<%}
if (item.FieldType == "CheckBox")
{%>
<%=Html.CheckBox(item.Field, **item.Value**, new { tabindex = item.SortOrder })%>
<%}
你能解釋一下它變得有用的場景嗎?看起來你正在嘗試製作某種EAV系統。 – Neal 2009-12-21 20:25:21