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 6390453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:33:00+00:00 2026-05-25T03:33:00+00:00

I’m using Entity Framework Code First to generated my database, so I have an

  • 0

I’m using Entity Framework Code First to generated my database, so I have an object defined like the following:

public class Band
{
    public int Id { get; set; }
    [Required(ErrorMessage = "You must enter a name of this band.")]
    public string Name { get; set; }
    // ...
    public virtual ICollection<Genre> Genres { get; set; }
}

Now I’m looking at a create view for this and the default scaffolding isn’t adding Genres to my form, which from past experience is about what I expect.

Looking online I’ve found Using ASP.NET MVC v2 EditorFor and DisplayFor with IEnumerable<T> Generic types which seems to come closest to what I want, but doesn’t seem to make sense with Razor and possibly MVC 3, per ASP.NET MVC 3 Custom Display Template With UIHint – For Loop Required?.

At present I’ve added the listing of genres to the ViewBag and then loop through that listing in my create view:

@{
    List<Genre> genreList = ViewBag.Genres as List<Genre>;
}
// ...
<ul>
@for (int i = 0; i < genreList.Count; i++)
{
    <li><input type="checkbox" name="Genres" id="Genre@(i.ToString())" value="@genreList[i].Name" /> @Html.Label("Genre" + i.ToString(), genreList[i].Name)</li>
}
</ul>

Outside of not yet handling cases where the user has JavaScript disabled and the checkboxes need to be re-checked, and actually updating the database with this information, it does output the genres as I’d like.

But this doesn’t feel right, based on how good MVC 3 has become.

So what’s the most effective way to handle this in MVC 3?

  • 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. Editorial Team
    Editorial Team
    2026-05-25T03:33:01+00:00Added an answer on May 25, 2026 at 3:33 am

    I don’t send lists into my View via the ViewBag, instead I use my viewmodel to do this. For instance, I did something like this:

    I have an EditorTemplate like this:

    @model IceCream.ViewModels.Toppings.ToppingsViewModel
    <div>
       @Html.HiddenFor(x => x.Id)
       @Html.TextBoxFor(x =x> x.Name, new { @readonly="readonly"})
       @Html.CheckBoxFor(x => x.IsChecked)
    </div>
    

    which I put in my Views\IceCream\EditorTemplates folder. I use this to display some html for allowing the user to “check” any particular topping.

    Then in my View I’ve got something like this:

    @HtmlEditorFor(model => model.Toppings)
    

    and that will use that result in my EditorTemplate being used for each of the toppings in the Toppings property of my viewmodel.

    And then I’ve got a viewmodel which, among other things, includes the Toppings collection:

    public IEnumerable<ToppingsViewModel> Toppings { get; set; }
    

    Over in my controller, among other things, I retrieve the toppings (however I do that in my case) and set my viewmodel’s property to that collection of toppings. In the case of an Edit, where toppings may have been selected previously, I set the IsChecked member of the TopingsViewModel and it’ll set the corresponding checkboxes to checked.

    Doing it this way provided the correct model binding so that when the user checked a few toppings, the underlying items in the collection reflected those selections. Worked well for me, hope it’s helpful for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.