ASP.NET MVC: Best way to get form checkboxes into many-to-many DB assoc table with LINQ To SQL?

前端 未结 1 740
时光说笑
时光说笑 2021-02-01 09:07

I have an ASP.NET MVC view which contains checkboxes for user-defined categories.

<% foreach (Category c in (List)ViewData[\"cats\"]         


        
相关标签:
1条回答
  • 2021-02-01 09:49

    I think what you're doing is fine... the only thing I would say is that if you don't want to loop through the ID's, you could add them at the same time with "InsertAllOnSubmit" (even though this is really just looping too):

    int[] categoryIDs = new int[] { 0, 1, 2 };
    
    dataContext.PostCategories.InsertAllOnSubmit(
    categoryIDs.Select(id =>
        new PostCategory
        {
            CategoryID = id,
            PostID = myPostID
        })
    );
    

    But either way, that's still just doing the same as what you have.

    0 讨论(0)
提交回复
热议问题