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.

-------------Problems Reply------------

I would recommend you reading the following blog post which explains in details how to achieve what you are looking for. mvc Views:0 Time:2010-12-01

Related post

  • Code equivalent to += assignment to an event 2009-08-03

    I was wondering if anyone could tell me the raw code equivalent to the += operator for adding a method to an event. I am curious to how it it works from a technical standpoint. --------------Solutions------------- An event defines a set of methods in

  • Code equivalent of XAML snippet 2010-12-16

    I want to know the code equivalent of the part that is inside the TextBlock: <TextBlock> Hello <Run Background="Red">S</Run> <Run Background="Blue">O</Run> </TextBlock> The reason is that I have a converter that re

  • Code equivalent for size property in Interface Builder 2011-10-07

    I'm creating some NSTableColumns dynamically and they appear too tall in the table. In the Interface Builder there is a general setting to adjust the object size (mini, small, regular). Is there any code equivalent for this or should I simply select

  • What is code equivalent of XAML to preserve whitespace? 2011-11-12

    What is c# code equivalent of following XAML, where I have a RichTextBox and I have selected Paragraph, and I want to enable/disable white space on this paragraph. In XAML I know how to enable, but I need to do this in code. <Paragraph xml:space=\

  • Code equivalent to the 'let' keyword in chained LINQ extension method calls 2009-07-07

    Using the C# compilers query comprehension features, you can write code like: var names = new string[] { "Dog", "Cat", "Giraffe", "Monkey", "Tortoise" }; var result = from animalName in names let nameLength = animalName.Length where nameLength > 3

  • Code equivalent for android:password="false" 2011-05-24

    I'd like to ask a simple question, what is the equivalent android code for android:password="false" on the xml layout file. (I googled it but can't find the result:) ) Thank you --------------Solutions------------- The equivalent to android:password=

  • Trying to encode a letter into its morse-code equivalent 2011-06-26

    So I am making a program that can take a string and encode that string into morse code. And vice versa too. I am having a problem in my charToMorse method though in mc_tree.cpp. Letters and their morse-code equivalents are stored in a tree in the ord

  • c++ code equivalent since c#? 2011-08-18

    How do you code this code in c++? public bool returnboolean(bool x) { return x; } public double sum(double one, double two) { return one+two; } public string yourname(string name) { return "hello "+yourname; } Where do i go to write code? in *.h? or

  • Java code equivalent to following php code 2011-10-05

    What could be the java equivalent code for following php syntax: $newPerson = array( 'firstname' => 'First', 'lastname' => 'Last', 'email' => '[email protected]', ); I think here firstname is index of array and First is value at that index.Ho

  • How do you convert a fluent nHibernate dynamic property mapping to the NH 3.2 mapping by code equivalent 2011-11-01

    I have the following mapping in fluent nHibernate: public CustomFieldsMap() { Schema("schema"); Table("table"); Id(x => x.Id, m => m.Column("id")); DynamicComponent(x => x.Fields, c => { ...insert code here... }); } This works fine (with

  • Markitup picture upload php source code equivalent in 2012-02-18

    I need equivalent of this source code block. /* The following is a very basic PHP script to handle the upload. One might also * resize the image (send back the *new* size!) or save some image info to a * database. Remember that you can modify

  • C# to JAVA Code Equivalent (Remove Extra Space within the String) 2012-03-06

    What is the equivalent for this C# code in Java? String newSTr = string.Join(" ", xString.Split(new []{' '}, StringSplitOptions.RemoveEmptyEntries)); Basically the function trims all extra spaces (trailing and spaces inside within the string). Thanks

  • Why shouldn't I use spaces in file names when I can use a code equivalent? 2009-07-07

    I've read all over the place NOT to use spaces in file names intended for use on the Web, e.g. URLs. I don't understand what's wrong with using "%20" to replace them when referring to the name in my code. It seems more appropriate to use this replace

  • Are the two codes equivalent to each other? 2010-06-22

    i know ive asked similar questions like this before, but: Is this pseudocode here the same as my code? upper case variables are the variables in the pseudocode with " ' " and the values with conditions are all in lists, such as: all "s" conditions ar

  • What's the code equivalent to the autoresizing mask anchoring options in IB? 2010-08-10

    How can I get a NSTextField (or any object) to respect the autoresizing options below via code? I want to add new objects to the view dynamically, but when I increase the height of the view, they are anchored to the bottom left, as opposed to the top

  • C# code equivalent for Applying RowValidationRules on DataGrid in WPF 2011-01-24

    We can apply RowValidationRules on a DataGrid by using following XAML. <DataGrid.RowValidationRules> <local:MyValidationClass ValidationStep="UpdatedValue"/> </DataGrid.RowValidationRules> I want to know how can I achieve same throu

  • Convert all characters to dec or hex HTML code equivalent 2011-05-31

    Is there a way I can convert ALL characters, including regular ones, using PHP to something like this: & é à ç < When I say regular characters i mean to say chars like "[email protected]#$", etc. Is this possible? Thanks in advanced! --------------Sol

  • Is there a javascript function that converts characters to the &code; equivalent? 2011-12-01

    I have text that was created created with CKeditor, and it seems to be inserting &nbsp; where spaces should be. And it appears to do similar conversions with >, <, &, etc. which is fine, except, when I make a DOMSelection, those codes a

  • Replacing the character period to morse code equivalent messes up the TextBox 2011-12-15

    I'm trying to replace all of the characters to their morse code equivalents. I used the textBlock.Text.Replace() method to accomplish this and it worked fine until I reached the period character. When I try to replace it with ".-.-.-", it works fine

Copyright (C), All Rights Reserved.

processed in 0.093 (s). 11 q(s)