开发者

ASP.NET MVC: How to POST complex object with Lists to server for Update (with/without FormCollection)

I've seen this question posted a couple other times on SO, but not in quite the same context as mine. So I apologize if this is a re-post.

I have the following complex object:

public class UserProfile
{
    public Education education { get; set; }
    // 3 other properties irrelevant to question b/c not able to be updated
}

The Education class looks like this:

public class Education
{
    public List<Certification> certifications { get; set; }
    public List<Degree> degrees { get; set; }
}

And the Certification class looks like this:

public class Certification
{
    public int yearCertified { get; set; }
    public DateTime expirationDate { get; set; }
}

The Degree class looks similar to the Certification class. On my Edit (Update) view page for their User Profile, I allow the user to dynamically add as many Degrees and Certifications as he/she wants. This means that I'd like to POST a list of Certification and Degree objects to the Update method in my controller.

I know posting simple objec开发者_运维知识库ts is easy with MVC, but trying to post a complex object for an Update, especially with a List of other objects, is a little more tricky.

Can anyone help me with what my View and Controller should look like? I'm using MVC 1.0.

Thank you.


I am working with something similar and i solved using a combination of the model and FormCollection.

Assuming that your action signature is:

[AcceptVerbs("Post")]
public ActionResult Edit(Education edu, FormCollection formColl)
{
  var tmp = formColl;
  return view...
}

I set a break point on var tmp, verify which properties were populated by the framework, then use variables posted in FormCollection to set the rest.

surly there are better ways to solve this issue, but this is something that used and might help your case.

Regards

Thabet

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜