ASP.NET MVC 3 - editing elements dynamically added to the model collection in the jquery dialog

I am new to MVC, so I was not sure what the best approach would be here.

I have a presentation model that contains several such collections:

public class MainViewModel{ public List<AViewModel> A { get; set; } public List<BViewModel> B {get; set; } ...} 

I use the Steve Sanderson approach here to dynamically add items to the collection, and it works fine while the children are being edited on the main view.

I'm having a problem returning a read-only list with an edit link that will open the details for editing in a pop-up dialog box.

Since these elements can be recently added, I cannot use the ID property to return a partial view from the controller. It looks like I have to display editors in a hidden div like this:

  <div class="AEditorRow"> @using (Html.BeginCollectionItem("A")) { @Html.DisplayFor(l => l.ID) @Html.DisplayFor(l => l.Name) @Html.DisplayFor(l => l.Code) <a href="#" onclick="$('#detailsPopup').html($(this).parent().find('.ADetails').html() ).dialog()">edit</a> <text>|</text> <a href="#" class="deleteRow">delete</a> <div class="ADetails" style="display: none"> @using (Html.BeginForm("EditA", "Controller")) {<fieldset> <legend>Location</legend> @Html.HiddenFor(model => model.ID) <div class="editor-label"> @Html.LabelFor(model => model.Code) </div> 

Does anyone know how best to do this?

+6
source share
2 answers

After working on this issue for a while, I was able to find a walkthrough that worked for me.

http://jarrettmeyer.com/post/2995732471/nested-collection-models-in-asp-net-mvc-3

I think this is the most applicable method for executing dynamically added nested collection objects for MVC3. Most of the other suggestions I found were for MVC2 or MVC1, and it seems like every MVC iteration is the best way to achieve this.

Hope this works for you.

+5
source

Source: https://habr.com/ru/post/888454/


All Articles