Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 83977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:51:30+00:00 2026-05-10T21:51:30+00:00

I have an ASP.NET MVC view which contains checkboxes for user-defined categories. <td><% foreach

  • 0

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

<td><% foreach (Category c in (List<Category>)ViewData['cats']) {        if (selCats.ContainsKey(c.ID)) { %>        <input name='CategoryIDs' type='checkbox' value='<%=c.ID %>' checked='checked' />&nbsp;<%= c.Name%><% }          else { %>        <input name='CategoryIDs' type='checkbox' value='<%=c.ID %>' />&nbsp;<%= c.Name%> <% } %>      <% } %> </td> 

The form is posted to the following controller action:

[AcceptVerbs(HttpVerbs.Post)] public ActionResult Edit(int id, int [] CategoryIDs) {     PostsDataContext db = new PostsDataContext();     var post = db.Posts.Single(p => p.ID == id);     UpdateModel(post, new[] { 'Title', 'Subtitle', 'RawContent', 'PublishDate', 'Slug' });     db.SubmitChanges();     return Redirect('/archives/' + post.Slug); } 

The form checkboxes will be converted to an array of integer IDs ‘CategoryIDs’, each representing a selected category. I then want to put these into a association table containing two columns: PostID and CategoryID. This is used to set up a many-to-many association between Posts and Categories.

Currently I am brute-forcing it: looping through the categories, and for each one, adding a row to the association table containing the category ID, and the ID of the post to which it belong.

But I’m wondering if there’s a cleaner way to do this automagically in the context of ASP.NET MVC and LINQ to SQL?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T21:51:31+00:00Added an answer on May 10, 2026 at 9:51 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom view engine in ASP.NET MVC and since upgrading to RC1
I have an ASP.NET MVC-application which I want deployable on both IIS6 and IIS7
I have an ASP.NET MVC application, when a user clicks on the submit button
I have an asp.net mvc application with a route similar to: routes.MapRoute(Blog, {controller}/{action}/{year}/{month}/{day}/{friendlyName}, new
I have an ASP.NET MVC (Beta 1) website that I'm using themes with. When
I have an ASP.Net MVC Ajax.BeginForm that submits and updates my page properly when
I have an ASP.NET MVC project with a form. In the Action method that
I have an asp.net mvc app running on a local iis website that is
I have an ASP.NET MVC project and I have a single action that accepts

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.