1
public class PersonViewModel
{
 Public string PersonName{get; set;}
 Public string NumberOfCars{get;set;} // DropDown
}
Public Class CarViewModel
{
 Public string CareMake{get; set;}
Public string  Year{get; set;}
}

On a Page I have to display PersonViewModel related controls. Based on Number Of Cars selected( 2 or 3) I should be showing Input fields for CarViewModel (Which is an Editor Template)

Problem: I have written Editor Template for CarViewModel. Not sure how to call it dynamically based on drop down selected (i.e., NumberOfCars selected by person).

PS

  • I can Create Partial views and render multiple times dynamically based on dropdown selection. But not sure How to model bind child(CarViewModel) to Parent ViewModel which is PersonViewModel in case.
  • I haven't written all code for easy understanding of problem.
Rajesh
  • 2,472
  • 3
  • 25
  • 31
  • Is the idea that if you select 3 then 3 sets or controls will be created to add 3 new cars? And what do you mean bind to the parent PersonViewModel? `PersonViewModel` does not contain a property `IEnumerable` –  Apr 24 '15 at 11:16
  • @StephenMuecke Yes. When I select 3 then 3 sets or controls will be created to add 3 new cars. While doing post back How can I do model binding. The only way I see to to post data is through Jquery. – Rajesh Apr 24 '15 at 15:00
  • If `PersonViewModel` contained property `List Cars` then you would call a server method that initializes a new `PersonViewModel` with 3 default `Car` objects in the list and return a partial view containing the controls for the 3 cars. But a more typical approach is just to have an 'Add car` button and dynamically add new cars. A pure client side approach is [shown in this answer](http://stackoverflow.com/questions/29837547/set-class-validation-for-dynamic-textbox-in-a-table/29838689#29838689) –  Apr 25 '15 at 00:17

0 Answers0