0
任何人都知道如何從Controller中調用與Html.EditorFor()等效的函數?Html.EditorFor()的等效代碼
我在我的視圖中有一個標準表。該行使用的EditorTemplate
<table id="tableOfBar">
<tfoot>
<tr><td colspan="2">Some paging controls and whatnot</td></tr>
</tfoot>
<tbody>
<% for (int i=0;i<Model.Rows.Count();i++)
{
int j = i;
%><%: Html.EditorFor(m => m.Rows[j], "FooRowTemplate", "Rows["+j+"]", new { OtherViewDataStuff})%>
</tbody>
</table>
編輯模板是一個強類型,返回<tr>
和子元素局部視圖。
所有這些工作正常,但下一步是添加一個「添加行」按鈕。出於一系列超出範圍的原因,我想打擊服務器而不是純粹的客戶端解決方案。我想要做的是將EditorTemplate作爲部分視圖返回,並使JavaScript返回Html並將其附加到表中。
[HttpPost]
public ActionResult AddRow(FooModel model)
{
var row = new FooRowModel();
model.AddRow(row);
var index = model.Rows.IndexOf(row);
return PartialView("~/Areas/MyArea/Views/Shared/EditorTemplates/FooRowTemplate.ascx", model.Rows[index]);
}
除了輸入命名之外,所有這些工作都很好。缺少的部分是集合前綴,它告訴模型活頁夾如何在回發時重新組裝模型。在視圖方面,您可以指定應該是什麼(這是上述EditorFor()
調用的"Rows["+j+"]"
部分)。
你能給部分視圖一個像EditorFor()一樣的html字段名稱前綴嗎?有關如何使這項工作的任何想法?我的整個方法可能完全錯誤,沒關係。告訴我爲什麼。 :)
感謝您的幫助。