Tony smith
Tony smith

Reputation: 49

How to add extra item in asp .net mvc dropdownlist from controller

I am using ViewModel to create view in my asp .net MVC application. In this view I need to bind a dropdownlist (Successfully binded). Problem: I want to add extra items with value in dropdownlist like "select","0"

Here is my Viewmodel:

 public class PagesViewModel
{
    public int Id { get; set; }

    [Required]
    [Display(Name = "Page Name")]
    public string PageName { get; set; }


    [Display(Name = "Parent Page")]
    public IEnumerable<Page> Page { get; set; }
}

My Controller:

 public ActionResult Create()
    {
        var model = new PagesViewModel
        {
            Page = db.Pages.Where(s => s.IsActive == true).OrderBy(r => r.PageName)

        };
        //--- Here I need to add extra items manually
        return View(model);
    }

My View:

  @Html.DropDownListFor(model => model.Id, new SelectList(Model.Page, "Id", "PageName"), "Select Parent Page", htmlAttributes: new { @class = "form-control" })

Upvotes: 1

Views: 8589

Answers (3)

Tieson T.
Tieson T.

Reputation: 21191

There is an overload for the DropDownListFor helper that allows you to specify an option label - I assume that is what you are after.

@Html.DropDownListFor(m => m.SomeID, Model.SomeSelectList, "Select one...", new { @class="your-class-here" })

This will render a <option> element with a value="" attribute:

<select id="SomeId" name="SomeId" class="your-class-here">
    <option value="">Select one...</option>
    <!-- ... the rest of the items from the SelectList -->
</select>

Otherwise, your Pages property needs to be something that derives from ICollection (like a List), which will allow you to add items:

Model:

public class PagesViewModel
{
    public int Id { get; set; }

    [Required]
    [Display(Name = "Page Name")]
    public string PageName { get; set; }


    [Display(Name = "Parent Page")]
    public ICollection<Page> Page { get; set; }
}

Controller:

public ActionResult Create()
{
    var model = new PagesViewModel
    {
        Page = db.Pages.Where(s => s.IsActive == true)
            .OrderBy(r => r.PageName)
            .ToList()
    };

    model.Pages.Add(new Page{ /* ... object initializers here */ });

    return View(model);
}

Upvotes: 3

Sandip Jaiswal
Sandip Jaiswal

Reputation: 3720

public ActionResult Create()
    {
        var model = new PagesViewModel
        {
            Page = db.Pages.Where(s => s.IsActive == true).OrderBy(r => r.PageName)

        };
List<SelectListItem> listItem = new List<SelectListItem>();
foreach(var item in Model)
{
    listItem.Add(new SelectListItem{Text = item.PageName, Value=item.Id});
}
listItem.Add( new SelectListItem{Text = "SomeText", Value="Some Value"});
        //--- Here I need to add extra items manually
        return View(model);
    }

you can also insert your optional text at top by linq extension method Insert(index, ValueToBeInserted). index will be zero if you want insert at top.

Please mark as anser if helped.

Upvotes: 1

Nilesh Gajare
Nilesh Gajare

Reputation: 6398

try this

     var model = new PagesViewModel
            {
                Page = db.Pages.Where(s => s.IsActive == true).OrderBy(r => r.PageName)

            };
model.Page.Add(new Page{ Name='ABC' ... }) // You can define your properties here

Upvotes: 1

Related Questions