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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:40:51+00:00 2026-05-23T17:40:51+00:00

I have MVC3 web application where we need to populate radio button list with

  • 0

I have MVC3 web application where we need to populate radio button list with validation. My Model is something like this:

public class EmployeesViewModel
{
     public List<Employee> listEmployee { get; set; } //To persist during post
     public IEnumerable<SelectListItem> selectListEmployee { get; set; }

     [Required]
     public Employee selectedEmployee { get; set; }
}

public class Employee
{
  public int ID {get; set;}
  public string Name {get; set}
  public string Department {get; set}
 }

I need to populate radiobutton list something like below:

  • Employee1ID – Employee1Name – Employee1Department // id – name – department
  • Employee2ID – Employee2Name – Employee2Department
  • Employee3ID – Employee3Name – Employee3Department

Selected Employee should be stored into “selectedEmployee” field. What is the best or clean way to populate these radio button List in MVC3?

Note: Mainly Looking for two task:
1. storing “Employee” object in each “Input” radio button tag, so that selected employee will be saved to “selectedEmployee” field
2. Best way to mark “Employee” object as required field

Much appreciate your help!

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. Editorial Team
    Editorial Team
    2026-05-23T17:40:52+00:00Added an answer on May 23, 2026 at 5:40 pm

    Here’s what I would recommend you. Start with a clean view model, one that really expresses what the view contains as information:

    public class EmployeesViewModel
    {
        public List<EmployeeViewModel> ListEmployee { get; set; }
    
        [Required]
        public int? SelectedEmployeeId { get; set; }
    }
    
    public class EmployeeViewModel
    {
        public int ID { get; set; }
        public string Label { get; set; }
    }
    

    then a controller:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var model = new EmployeesViewModel
            {
                ListEmployee = GetEmployees()
            };
            return View(model);
        }
    
        [HttpPost]
        public ActionResult Index(EmployeesViewModel model)
        {
            if (!ModelState.IsValid)
            {
                // the model is invalid, the user didn't select an employee
                // => refetch the employee list from the repository and
                // redisplay the view so that he can fix the errors
                model.ListEmployee = GetEmployees();
                return View(model);
            }
    
            // validation passed at this stage
            // TODO: model.SelectedEmployeeId will contain the id
            // of the selected employee => use your repository to fetch the
            // actual employee object and do something with it 
            // (like grant him the employee of the month prize :-))
    
            return Content("thanks for submitting", "text/plain");
        }
    
        // TODO: This doesn't belong here obviously
        // it's only for demonstration purposes. In the real 
        // application you have a repository, use DI, ...
        private List<EmployeeViewModel> GetEmployees()
        {
            return new[]
            {
                new EmployeeViewModel { ID = 1, Label = "John (HR)" },
                new EmployeeViewModel { ID = 2, Label = "Peter (IT)" },
                new EmployeeViewModel { ID = 3, Label = "Nathalie (Sales)" },
            }.ToList();
        }
    }
    

    and finally a view:

    @model EmployeesViewModel
    
    @using (Html.BeginForm())
    {
        @Html.ValidationMessageFor(x => x.SelectedEmployeeId)
        @foreach (var employee in Model.ListEmployee)
        {
            <div>
                @Html.RadioButtonFor(x => x.SelectedEmployeeId, employee.ID, new { id = "emp" + employee.ID })
                @Html.Label("emp" + employee.ID, employee.Label)
            </div>
        }
        <input type="submit" value="OK" />
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this web app created using MVC3 and need to make a Windows
I need to develop a web application, i have two choices mvc3 straight forward
we are developing a web application using MVC3 and Jquery. we have a situation
I have an MVC 3 web application. I need to step through the controller
I have MVC3 web application running under IIS 7. During initialization in global.asax application
I'm a bit new to SSRS... I have an MVC3 application which I need
I would like to build a new web application using ASP.NET MVC3 and MongoDB.
I have a mvc3 web application which uses jqgrid extensively. I just came to
I have an ASP.NET MVC3 web application with UI, Business (entities), and Data (DbContext)
I have a web application that has a grid displaying a paged list of

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.