Tech Off Thread

5 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

MVC 2 Create/Edit many to many scenarios

Back to Forum: Tech Off
  • User profile image
    MasterPi

    Hey all!

     

    How do you guys construct Create views/action methods for many to many scenarios?

    Given data modeled as Books, BookAuthors, Authors...

    I want to be able create a new book and create or associate authors (for now, just associating existing ones) for that book all on the same create page.

     

    I understand I can just enumarate the FormCollection and find all specific keys that match "Authors", but that all seems tedious and messy. Google either points to that solution or to various one to many solutions...you can use TryUpdateModel(media.Authors, "Authors") if you have a one to many : Book to Authors schema.

     

    Do you guys create your create views and controller actons in any specific manner to do something like I outlined above?

     

    Even just a simple way of returning a strongly typed collection of Authors from the form along with the Book object would help greatly.

     

    Thanks!

  • User profile image
    Maddus Mattus

    It's very tricky in MVC

     

    basically you have to do:

     

    <%= Html.Input("Model.Authors[0].FirstName", Model.Authors[0].FirstName) %>

     

    that will render

     

    <input name="Model.Authors[0].FirstName" id="Model.Authors[0].FirstName" value="" />

     

    Then you can have a BookModel that looks like

     

    public class BookModel

    {

    public AuthorModel[] Authors { get; set; }

    }

     

    public class AuthorModel

    {

    public string FirstName { get; set; }

    }

     

    But personally, I use this as a last resort. Either make the user enter an author first, before entering book information.

  • User profile image
    MasterPi

    Maddus Mattus said:

    It's very tricky in MVC

     

    basically you have to do:

     

    <%= Html.Input("Model.Authors[0].FirstName", Model.Authors[0].FirstName) %>

     

    that will render

     

    <input name="Model.Authors[0].FirstName" id="Model.Authors[0].FirstName" value="" />

     

    Then you can have a BookModel that looks like

     

    public class BookModel

    {

    public AuthorModel[] Authors { get; set; }

    }

     

    public class AuthorModel

    {

    public string FirstName { get; set; }

    }

     

    But personally, I use this as a last resort. Either make the user enter an author first, before entering book information.

    And would BookModel.Authors automatically collect the Model.Authors[i] defined in the html? Also, (I forgot to mention I'm using linq 2 sql) would a linq 2 sql insert on submit on the Book insert the authors as well or would I still have to loop through each one and insert?

  • User profile image
    Maddus Mattus

    MasterPie said:
    Maddus Mattus said:
    *snip*

    And would BookModel.Authors automatically collect the Model.Authors[i] defined in the html? Also, (I forgot to mention I'm using linq 2 sql) would a linq 2 sql insert on submit on the Book insert the authors as well or would I still have to loop through each one and insert?

    What do you mean with automatically collect?

     

    Don't use LINQ2SQL for your models in MVC.

     

    Those objects are context sensitive and you will get very hairy behaviour and pull out hair situations, if you do.

  • User profile image
    MasterPi

    Maddus Mattus said:
    MasterPie said:
    *snip*

    What do you mean with automatically collect?

     

    Don't use LINQ2SQL for your models in MVC.

     

    Those objects are context sensitive and you will get very hairy behaviour and pull out hair situations, if you do.

    "What do you mean with automatically collect?"

     

    Well, you defined the html for the authors stuff in a specific manner, so I was wondering if in the controller create/update action, the model will now store the data for the authors the user inputted in the AuthorModels Authors[] collection .

     

    "Don't use LINQ2SQL for your models in MVC."

     

    Do you suggest I create view models and then write specific methods for updating and loading viewmodels with linq to sql as backend? If so, I was going to do that anyway. Tongue Out

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.