Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

In my View I have to put several render partial pages to dynamically add rows in a table (eg. product line in an invoice)

To add the first table I have followed: http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/

I have followed the instructions in the link but my main view looks like this:

<table id="editorRows" style="width: 100%;">
  <tr>
    <th>Column1</th>
    <th>Column2</th>
    <th>Column3</th>
  </tr>

  @foreach (var item in Model.Entity.ListPlans)
  {
  Html.RenderPartial("PlanEditorRow", item);
  }

</table> 
<b><i>@Html.ActionLink("Add another plan...", "BlankPlanEditorRow", null, new { id = "addItem" })</i></b>

The partial view (PlanEditorRow) is:

@model Project.Models.Plan
@using Project.Helpers

<tr class="editorRow">
@using (Html.BeginCollectionItem("assignedPlans"))
{

    <td>
        @Html.EditorFor(x => x.Data1)
    </td>
    <td>
        @Html.EditorFor(x => x.Data2)
    </td>

    <td>
        @Html.EditorFor(x => x.Data3)
    </td>
    <td>
        <a href="#" class="deleteRow">delete</a>
    </td>
}
</tr>

I use the same javascript as in the link above. When clicking the add plan link, the plans editor rows in the table are correctly added dynamically on the same main view within the form.

But when I add a similar second table for eg. products with changed renderpartial configurations and click on add link the editor row is loaded on a new page (ie. partial view only, the main view is gone). I use the same ID and classes from the first table and same JS.

I guess the HTML element ID can't be the same, if so how can I solve it?

How can I make sure any number of similar tables in the form, function correctly as with first table in terms of dynamic row addition?

@Chris Pratt, I have already used this:

$("#addItem").click(function () {
$.ajax({
    url: this.href,
    cache: false,
    success: function (html) { $("#editorRows").append(html); }
});
return false;
});

$("a.deleteRow").live("click", function () {
$(this).parents("tr.editorRow:first").remove();
return false;
});

Not sure if this what you mean with adding html via AJAX when requesting partial?

share|improve this question
add comment

1 Answer

You can solve it by actually using JavaScript for this. You're just linking to an action and that action is returning just the rendered partial, which is why you lose everything else. You must request the partial via AJAX and then add the HTML to the page.

share|improve this answer
    
Ok I have added the JS showing what I think you mean...Please let me know otherwise –  PGup Jan 29 at 23:20
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.