How to extend the DropDownListFor() in MVC

JohnMighty

I'm trying to extend DropDownListFor in such a way that to preserve the original functionality, but add the functionality that if the selected value that is given is Null, to a new value to the SelectListItem list such as 'Select an item'.

How would you do that?

EDIT: (I wasn't clear at the beginning)

If we look at the default DropDownListFor behavior, the extension gets a list of SelectItems and the 'selected' value. In my app, sometimes the 'selected' value is Null, thus no option is selected from the SelectItems list. I would like to change the default behavior in such a way, that if my 'selected' value is Null, then the DropDown will add automatically a new value such as 'Select an Item' and select it as the 'selected'.

Hope it's better now :)

Thanks

JohnMighty

OK, Did it! For future reference here is the solution:

I've create an extension method for the DropDownListFor:

public static MvcHtmlString KeywordDropDownListFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression,
                                                   IEnumerable<SelectListItem> selectList, object htmlAttributes)
{
    Func<TModel, TValue> method = expression.Compile();
    string value = method(helper.ViewData.Model) as string;

    if (String.IsNullOrEmpty(value))
    {
        List<SelectListItem> newItems = new List<SelectListItem>();
        newItems.Add(new SelectListItem
        {
            Selected = true,
            Text = Strings.ChooseAKeyword,
            Value = String.Empty
        });
        foreach (SelectListItem item in selectList)
        {
            newItems.Add(item);
        }

        return helper.DropDownListFor(expression, newItems, htmlAttributes);
    }

    return helper.DropDownListFor(expression, selectList, htmlAttributes);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to extend the DropDownListFor() in MVC

From Dev

DropDownListFor MVC : How to make it work

From Dev

MVC DropDownListFor how to add RouteLink

From Dev

How to get the value of a DropDownListFor in MVC6

From Dev

How to populate an dropdownlistfor from database in ASP.NET MVC 4

From Dev

How to set Default value in MVC 4 razor DropDownListFor

From Dev

How to select default value for DropDownListFor using ASP.NET MVC

From Dev

Asp .Net MVC, how to put/include values to DropDownListFor?

From Dev

MVC Entity Framework DropDownListFor<>

From Dev

MVC Entity Framework DropDownListFor<>

From Dev

Trouble with MVC 4 DropDownListFor

From Dev

MVC MultipleModel DropdownlistFor SaveChanges()

From Dev

How to extend Spring MVC Controller Annotation?

From Dev

How to extend Spring MVC Controller Annotation?

From Dev

MVC 5 DropDownListFor Database Linq;

From Dev

MVC DropDownListFor Bind on Nested Class

From Dev

DropDownListFor ASP MVC4

From Dev

ArgumentNullException on MVC5 DropdownListFor

From Dev

how to set Enum in DropDownListFor and keep selectet item when back to this page in mvc?

From Dev

asp.net mvc Html.DropDownListFor: how to handle selected id

From Dev

How to extend middleware to custom classes outside the MVC scope (Rails)?

From Dev

How to extend my web app framework (MVC) to serve a mobile client?

From Dev

How can I extend the HttpCookie object in MVC5?

From Dev

Cascading DropDownListFor ASP.NET MVC

From Dev

Dropdownlistfor MVC ASP.Net VB

From Dev

Getting selected Index from DropDownListFor MVC

From Dev

C# MVC DropDownListFor List of Strings

From Dev

MVC DropDownListFor - Selected Value ViewModel Class

From Dev

Set @Html.Dropdownlistfor to session in MVC 4

Related Related

  1. 1

    How to extend the DropDownListFor() in MVC

  2. 2

    DropDownListFor MVC : How to make it work

  3. 3

    MVC DropDownListFor how to add RouteLink

  4. 4

    How to get the value of a DropDownListFor in MVC6

  5. 5

    How to populate an dropdownlistfor from database in ASP.NET MVC 4

  6. 6

    How to set Default value in MVC 4 razor DropDownListFor

  7. 7

    How to select default value for DropDownListFor using ASP.NET MVC

  8. 8

    Asp .Net MVC, how to put/include values to DropDownListFor?

  9. 9

    MVC Entity Framework DropDownListFor<>

  10. 10

    MVC Entity Framework DropDownListFor<>

  11. 11

    Trouble with MVC 4 DropDownListFor

  12. 12

    MVC MultipleModel DropdownlistFor SaveChanges()

  13. 13

    How to extend Spring MVC Controller Annotation?

  14. 14

    How to extend Spring MVC Controller Annotation?

  15. 15

    MVC 5 DropDownListFor Database Linq;

  16. 16

    MVC DropDownListFor Bind on Nested Class

  17. 17

    DropDownListFor ASP MVC4

  18. 18

    ArgumentNullException on MVC5 DropdownListFor

  19. 19

    how to set Enum in DropDownListFor and keep selectet item when back to this page in mvc?

  20. 20

    asp.net mvc Html.DropDownListFor: how to handle selected id

  21. 21

    How to extend middleware to custom classes outside the MVC scope (Rails)?

  22. 22

    How to extend my web app framework (MVC) to serve a mobile client?

  23. 23

    How can I extend the HttpCookie object in MVC5?

  24. 24

    Cascading DropDownListFor ASP.NET MVC

  25. 25

    Dropdownlistfor MVC ASP.Net VB

  26. 26

    Getting selected Index from DropDownListFor MVC

  27. 27

    C# MVC DropDownListFor List of Strings

  28. 28

    MVC DropDownListFor - Selected Value ViewModel Class

  29. 29

    Set @Html.Dropdownlistfor to session in MVC 4

HotTag

Archive