ASP.net MVC Linq-To-SQL Extended Class Field Binding

Posted by user336858 on Stack Overflow See other posts from Stack Overflow or by user336858
Published on 2010-06-01T20:21:30Z Indexed on 2010/06/01 20:23 UTC
Read the original article Hit count: 181

Filed under:
|
|
|
|

Hi there,

The short version of this question is "Is there a way to get automatic View Object binding for fields defined in a partial class for a Linq-To-SQL generated class?" Apologies if it's been asked before.

Example
Suppose I have a typical MVC setup with the tables:

  • Posts {PostID, ...}
  • Categories {CategoryID, ...}

A post can have more than one category, and a category can identify more than one post. Thus suppose further that I need an extra table:

  • PostCategories {PostID, CategoryID, ...}

This handles the many-to-many relationship between posts and categories. As far as I know, there's no way to do this in Linq-to-SQL right now so I have to shoehorn it in by adding a partial Postclass to the project to add that functionality. Something like:

public partial class Post
{
   public IEnumerable<Category> Categories{ get { ... } set { ... } }
}

So here's my question:
If a user is accessing my MVC application front-end and begins editing a Post object, they might enter an invalid category. When the server recognizes the invalid input, the usual practice is to return the faulty object to the original view for re-editing along with some error messages.

The fields in the edit page are re-populated with the provided values. However I don't know how to get this mechanism to work with the properties I created with the partial class as shown above.

Any terminology, links, or tips you can provide would be tremendously helpful!

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET