开发者

Advanced Model Binding vs 2010

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

I have two classes Person and 开发者_如何学GoTest 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜