tags:

views:

32

answers:

1

Can I have an example of advanced model binding using ajaxpost?

I have two classes Person and Test as follows:

 public class person{
    public ArrayList Name = new ArrayList();
    public Test []test {get;set;}
    }

public class Test
    {
    public int ID{get;set;}
    public int mark{get;set;}
    }

My Controller could be

[HttpPost]
        public EmptyResult CaseTest(Person person)
        {
            return new EmptyResult();
        }

How do I post the values from the view?

+2  A: 

To fill the test collection property of your model you could have the following inputs:

<input type="text" name="test[0].ID" value="1" />
<input type="text" name="test[0].mark" value="123" />

<input type="text" name="test[1].ID" value="2" />
<input type="text" name="test[1].mark" value="456" />

As far as the ArrayList field is concerned you should use a generic strongly typed collection and use a property with getter and setter instead of field for better encapsulation.

I would also encourage you reading this blog post from Scott Hanselman in which he covers advanced binding scenarios.

Darin Dimitrov
Thanks Dariv. I have to use javascript to do as above and I have posted the remaining of the question on http://stackoverflow.com/questions/3036751/test-is-null-in-the-controller-upon-post.