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

Is it ever advantageous to use 'goto' in a language that supports loops and functions? If so, why?

c exception-handling language-agnostic goto

I've long been under the impression that goto should never be used if possible. While perusing libavcodec (which is written in C) the other day, I noticed multiple uses of it. Is it ever advantag...

General question on SQL - JOINS

sql sql-server

If I was asked to query JOIN of more than three tables, what is the best way I go about understanding the relationship of the tables before I code. Should I use Database Diagram in SQL Server, or w...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.