Code equivalent for Html.EditorFor()
Anyone know how you can call a functional equivalent to Html.EditorFor() from the Controller?
I have a standard table in my View. The rows make use of an 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>
The editor template is a strongly typed partial view that returns the <tr>
and child elements.
All this works fine, but the next step is adding an "Add Row" button. For a bunch of reasons that are out of scope here, I want to hit the server rather than a purely client-side solution. What I'd am trying to do is return the EditorTemplate as a Partial View and have JavaScript take that returned Html and append it to the table.
[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/Edi开发者_JAVA百科torTemplates/FooRowTemplate.ascx", model.Rows[index]);
}
All of THAT works fine EXCEPT the input naming. The part that is missing is collection prefix stuff that tells the Model Binder how to reassemble the model on post back. On the View side, you can specify what this should be (that is the "Rows["+j+"]"
part of the EditorFor()
call above).
Can you give a Partial View a html field name prefix like with EditorFor()? Any thoughts on how to make this work? My whole approach may be totally wrong, that's fine. Just tell me why. :)
Thanks for your help.
I would recommend you reading the following blog post which explains in details how to achieve what you are looking for.
精彩评论