Reputation: 5886
I am having a little trouble figuring how to do this.
here's my scenario
A User
must create a IncomeDeclaration
for his Business
.
But an IncomeDeclaration
includes many ActivityIncomeDeclarations
for each Activity
that his business is on.
I would like my form to include the fields for the new IncomeDeclaration
as well as the fields for the ActivityIncomeDeclarations
(which will be obviously related to that IncomeDeclaration
)....
I know that rubyonrails has a method called "accepts_nested_attributes_for" tgat would probably help me here..
Please help!
Upvotes: 2
Views: 404
Reputation: 105059
This blog post of mine can help you lots:
Asp.net MVC model binding to List<T>
But basically you will provide fields for your IncomeDeclaration
and then dynamically add ActivityIncomeDeclaration
fields as needed.
If you want to actually serialize the form you will have to follow instruction provided in the upper blog post.
But there's the second choice as well if you use jQuery. You could manipulate a complex JavaScript object that would be a copy of the same server side C# class like:
var incDec = {
Name: ""
// and other properties
ActivityIncomeDeclarations: []
};
Then while user would be adding those ActivityIncomeDeclarations you'd always simply call
incDec.ActivityIncomeDeclarations.push({
// properties
});
Then my other blog post will help you that can convert this object to a Dictionary that jQuery for instance is easily able to process:
Sending complex JSON objects to Asp.net MVC using jQuery
By using the prlogin the blog post you could then just easily do:
$.ajax({
url: "someURL",
type: "POST",
data: $.toDictionary(incDec),
success: function(data, status, xhr){
// process success
},
error: function(xhr, status, err){
// process error
}
});
Upvotes: 1
Reputation: 81680
This is very easy to achieve. ASP NET MVC is very compositional and you can use templates to achieve this but inheritance is not the way to go.
so solution is for IncomeDeclaration
to contain a list of ActivityIncomeDeclarations
and then do this:
for(int i=0; i< Model.Activities.Count; i++)
{
Html.EditorFor(Model.Activities[i]); // you need an editor template (and a display template) for ActivityIncomeDeclarations
}
More info:
http://www.asp.net/mvc/videos/mvc2-template-customization http://www.dalsoft.co.uk/blog/index.php/2010/04/26/mvc-2-templates/
Upvotes: 1