locked
About HTML Helper RRS feed

  • Question

  • User-1187675394 posted
                            <div>
    
                                @{
                                    List<SelectListItem> listItems = new List<SelectListItem>();
    
                                    string test = "test";
                                    string test2 = "test2";
    
                                    listItems.Add(new SelectListItem()
                                    {
                                        Value = test,
                                        Text = test,
                                        Selected = test == item.Test ? true : false
                                    });
    
                                    listItems.Add(new SelectListItem()
                                    {
                                        Value = test2,
                                        Text = test2,
                                        Selected = test2 == item.test2 ? true : false
                                    });
                                }
    
                                @Html.DropDownList("Status", listItems, "Select Item")
    
                            </div>
    						

    Question:
    Is it possibly to do it with HTML helper in relation to ASP.net MVC instead of having the code above?

    I tried finding a solution to it but it doesn't work.

    Thank you!

    Tuesday, August 22, 2017 2:18 PM

Answers

  • User2103319870 posted

    Is it possibly to do it with HTML helper in relation to ASP.net MVC instead of having the code above?

    You can try below implementation

    Create a folder called Helpers and then create a file called "DropdownlistHelper.cs"

    Add the below code to class file we created now

    public static class DropdownlistExtensions
        {
            public static MvcHtmlString CustomDropdownlist(this HtmlHelper helper, string name,string itemselected)
            {
                List<SelectListItem> listItems = new List<SelectListItem>();
    
                string test = "test";
                string test2 = "test2";
    
                listItems.Add(new SelectListItem()
                {
                    Value = test,
                    Text = test,
                    Selected = test == itemselected ? true : false
                });
    
                listItems.Add(new SelectListItem()
                {
                    Value = test2,
                    Text = test2,
                    Selected = test2 == itemselected ? true : false
                });
    
                return System.Web.Mvc.Html.SelectExtensions.DropDownList(helper, name, listItems);
    
            }
    
        }

    Now in your view first import the namespace.

    @using WebApplication1.Helpers

    Please note above namespace is as per my webapplication, you have to change this as per your application

    Then you can use the helper like below

    @Html.CustomDropdownlist("YourDropdowlistName","YourSelectedItemValue")

    Sample Usage

    @Html.CustomDropdownlist("Dropdowlist1","test2")

    Refrence: https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/views/creating-custom-html-helpers-cs

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 22, 2017 3:37 PM