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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:31:02+00:00 2026-05-19T22:31:02+00:00

Scenario: The Create action method will pass an uninitialized instance of Person to the

  • 0

Scenario:

The Create action method will pass an uninitialized instance of Person to the View.
The View will show a dropdown control with “–Select–” highlighted.
Since the property Sex is nullable, I cannot invoke the extension method.

How to fix this problem?

Model:

namespace MvcApplication1.Models
{
    public enum Sex { Male, Female };
    public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }

        [Required(ErrorMessage="Please select either Female or Male.")]
        public Sex? Sex { get; set; }
    }
}

Controller:

    public ActionResult Create()
    {
        var p = new Person();
        ViewBag.SelectList = p.Sex.Value.GetSelectList();//Source of error!
        return View(p);
    }

Partial View:

@model MvcApplication1.Models.Person
@Html.HiddenFor(model => model.Id)
<div class="editor-label">
    @Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.Name)
    @Html.ValidationMessageFor(model => model.Name)
</div>
<div>
    @Html.DropDownListFor(model => model.Sex, ViewBag.SelectList as SelectList,"--Select--")
</div>

Extension:

 public static class Utilities
    {
        public static SelectList GetSelectList<TEnum>(this TEnum obj)
        {
            var values = from TEnum x in Enum.GetValues(typeof(TEnum))
                         select new { Text = x.ToString(), Value = x };
            return new SelectList(values, "Value", "Text", obj);
        }
    }
  • 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-19T22:31:02+00:00Added an answer on May 19, 2026 at 10:31 pm

    Why don’t you make your extension method to accept a Nullable as a parameter? (This extension method looks like a code smell to me)

    public static class Utilities
    {
        public static object GetSelectList<TEnum>(this TEnum? obj)
            where TEnum : struct 
        {
            var values = from TEnum x in Enum.GetValues(typeof(TEnum))
                         select new { Text = x.ToString(), Value = x };
            return new SelectList(values, "Value", "Text", obj);
        }
    }
    

    and use without extracting value:

    ViewBag.SelectList = p.Sex.GetSelectList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What will happen if I create an Extension method (EM) with same signature as
I'm attempting to implement a Create action method for an ASP.NET MVC web application.
I have the following scenarios: Scenario: Create a game with valid information Given I
I used to create scenarios where in scenario name I explain what is scenario.
How would I create the following scenario that rails can provide for carrierwave, using
Here's the scenario: I have 2 tables: CREATE TABLE dbo.API_User ( id int NOT
How do i create aliases in c# Take this scenario class CommandMessages { string
I have the following scenario, I want to create a DataGrid and then populate
I need to create relationships for my entities, whereby I have the following scenario:
I am trying to create a generic formatter/parser combination. Example scenario: I have a

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.