I'm struggling myself trying to make my form work with my models...
So, I have 3 Models
Account
has_one -> Company
has_many -> Individuals -> has_one Document
So the account can have one company and many individuals with one document each.
When submit my forms I have the following action
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult OpenAnAccount(string area, [Bind(Exclude = "Id")] Website.Model.Account account)
{
var db = new DB();
var type = new int();
switch (area)
{
case "business": type = 1; break;
case "private": type = 2; break;
}
if (ModelState.IsValid)
{
try
{
account.Type = type;
db.Accounts.InsertOnSubmit(account);
db.SubmitChanges();
return View("OpenAnAccount/ThankYou");
}
catch
{
return View();
}
}
else
{
return View();
}
}
And on my form simple things like
%label{for='companyName'} <em>*</em>Company Name
%input{class='required' id='companyName' name='Company.Name' type='text'}
%label{for='companyTradeName'}Trading As (if Different)
%input{id='companyTradeName' name='Company.Trade' type='text'}
%label{for='companyRegistrationNumber'} <em>*</em>Company Registration Number
%input{class='required' id='companyRegistrationNumber' name='Company.Registration' type='text'}
%label{for='vatNumber'}VAT Number (if Applicable)
%input{id='vatNumber' name='Company.VAT' type='text'}
Which means I am naming the fields as Company.Name, Company.Registration, etc... and for the individuals I'm doing like this
%label{for ='firstName_0'} <em>*</em>First Name
%input#firstName_0.required.firstName{ name='Individuals[0].FirstName' type='text'}
%label{for='middleName_0'} Middle Name
%input#middleName_0{ name='Individuals[0].MiddleName' type='text'}
%label{for='lastName_0'} <em>*</em>Last Name
%input#lastName_0.required{ name='Individuals[0].LastName' type='text'}
as Individuals[0].FirstName, Individuals[0].LastName, etc
But what is happening is that I'm indeed getting the account generation with my company, but it's not working for the Individuals....its not doing anything with the database?
Anyone could help me on that?
I've checked some resources like this http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx but it doesn't work in my case I suppose, because it didn't change anything.
Thanks in advance
This answer seemed to be a straightforward way to do it: ASP.Net MVC - model with collection not populating on postback In case anyone needs it