1

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" })
Tony smith
  • 49
  • 3
  • 13

3 Answers3

2

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);
}
Community
  • 1
  • 1
Tieson T.
  • 20,774
  • 6
  • 77
  • 92
  • T Can we add new item to specific position? – Tony smith Jun 09 '16 at 09:15
  • I believe there is an Insert method, but I won't be able to verify that any time soon. – Tieson T. Jun 09 '16 at 09:17
  • Yes there is insert method but I am not able to use. Please help. I have tried this but it is giving me error: Page = db.Pages.Where(s => s.IsActive == true).OrderBy(r => r.PageName).ToList().Insert(0, new Page { Id = 0, PageName = "Parent Page" }) – Tony smith Jun 09 '16 at 09:20
  • I can't really give you a detailed response at the moment, but it looks like you're trying to insert a record into a DbSet - that won't work. Change the collection type to IList in your model, which will give you access to Insert https://msdn.microsoft.com/en-us/library/8zsfbxz8(v=vs.110).aspx – Tieson T. Jun 09 '16 at 09:25
  • Done: model.Page.Insert(0, new Page { Id = 0, PageName = "Text here" }); – Tony smith Jun 09 '16 at 09:44
1

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
Nilesh Gajare
  • 6,302
  • 3
  • 42
  • 73
1
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.

Sandip Jaiswal
  • 3,428
  • 2
  • 13
  • 15