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

  • SEARCH
  • Home
  • 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 7955199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:39:31+00:00 2026-06-04T03:39:31+00:00

Keywords: asp.net mvc 3, checkbox list, strongly typed view. So, please don’t suggest me

  • 0

Keywords: asp.net mvc 3, checkbox list, strongly typed view.

So, please don’t suggest me to use ViewBag or formcollection.

I am not new to web form development, but I am fairly new to asp.net MVC.

Suppose on a registration page, I have something like this:

A very simple registration form

What would the view model look like? Does the following look right?

public class RegistrationViewModel

{

// To store the selected state ID upon postback.
public int StateId {get; set; }  

// A list of states to populate the dropdown.
public Dictionary<int, string> States {get; set; } 

// To store the list of insurance companies the user has used before.
public int[] PriorInsuranceCompanies {get; set; } 

// A list of insurance companies to populate the check box list.
public Dictionary<int, string> InsuranceCompanies {get; set; }

}

I know how to create the view like I’ve shown in the screen shot, but I am not sure how in my controller I should capture the view model values of user input. Remember I prefer a strongly typed view. How does the mvc framework tell which is which? Do you have a minimal working sample to share (preferably in a complete runnable project)? Thank you.

  • 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-06-04T03:39:32+00:00Added an answer on June 4, 2026 at 3:39 am

    The approach you are using is correct. I agree with you in using always viewmodels and never ViewBag.

    In your viewmodel you should change your dictionary to MultiSelectList so you can have the selected values also.

    public IList<int> PriorInsuranceCompaniesSelected { get; set; }
    public MultiSelectList PriorInsuranceCompanies { get; set; }
    

    You then map the first field if some Ids are already selected (info that you get when loading data from your repo for example) and the second with all the values.

    From your controller in the Get part (just some code as an example):

      model.PriorInsuranceCompaniesSelected = new List<int>();
      var companies = repository.GetPriorInsuranceCompanies();
      //add to your PriorInsuranceCompaniesSelected the values already checked from your entity
      var entity = repository.GetEntityBy(id);
      if (entity.PriorInsuranceCompanies != null)
        foreach (var item in entity.PriorInsuranceCompanies)
          model.PriorInsuranceCompaniesSelected.Add(item.Id);
    
      var select = (from s in companies select new { Id = s.Id, Name = s.Name }).OrderBy(x => x.Name); //.ToList;
      model.PriorInsuranceCompanies = new MultiSelectList(select, "Id", "Name", model.PriorInsuranceCompaniesSelected);
    

    Then in your Html you will have an output like this

    @foreach (var item in Model.PriorInsuranceCompanies)
    {
       <label for="@item.Value" class="check">
       <input type="checkbox" id="@item.Value" name="PriorInsuranceCompaniesSelected" value="@item.Value" @(item.Selected ? "checked" : "") />@item.Text</label>
    }
    

    On post, the ModelBinder will map the correct objects to your model automagically. You simply have to check values in model.PriorInsuranceCompaniesSelected

    [HttpPost]
    public ActionResult MyForm(MyViewModel model)
    {
      if (ModelState.IsValid)
      {
        try
        {
          //your mapping code or whatever...
    
          //You do your things with the selected ids..
          if (model.PriorInsuranceCompaniesSelected != null && model.PriorInsuranceCompaniesSelected.Count > 0)
            entity.PriorInsuranceCompanies = repository.GetCompaniesBy(model.PriorInsuranceCompaniesSelected);
          else
            entity.PriorInsuranceCompanies = new List<Comapny>();
          repository.Save(entity);
    
          return RedirectToAction("Index");
        }
        catch (RulesException ex)
        {
          ex.CopyTo(ModelState);
        }
        catch
        {
          ModelState.AddModelError("", "My generic error taken form a resource");
        }
      }
    
      //rehydratates the list in case of errors
      //....
      return View(model);
    }
    

    This should give you an idea of what to do. I hope it helps

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

Sidebar

Related Questions

I'm developing an web app using asp.net mvc. I have a List in Cache
Using asp.net MVC I'd like to do this inside a view: <%= Html.TextBox(textbox1, null,
In ASP.NET MVC, how to get the page content before render, we know in
I have read Scott Guthrie's Blog - ASP.NET MVC 3: New @model keyword in
I'm building out a simple simple ASP.NET MVC 2 page. Was able to do
I am following the Nerd Dinner tutorial as I'm learning ASP.NET MVC, and I
In my ASP.NET MVC project, I have a polymorphic collection that I wish to
I am playing around with the ASP.NET MVC Html.Helpers and I noticed that say
I'm developing a website (using asp.net-mvc) with a SqlServer 2005 database. I have numerous
For a scenario, I have a ASP.NET MVC application with URLs that look like

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.