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/EditorTemplates/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.

Answers


I would recommend you reading the following blog post which explains in details how to achieve what you are looking for.


Need Your Help

qDeleteAll on nested containers

c++ qt

In Qt if I have nested QMaps similiar to the following:

Socket connection - Blocked ports on network carrier

android sockets

I am developing an Android application that uses socket connections as part of its messaging system. However I have hit a problem that all ports (except the standard email/http etc ones) seems to be