1

I want to create a dropdownlist, but am struggling with how I display it.

I have the following code.

Controller

            var trader = new DisplayTradersAttachedToCategoryViewModel
            {
                Description = data.Description,
                Id = data.Id,
                BusinessName = traders.Select(x => new BusinessNameViewModel { BusinessName = x.BusinessName, Id = x.Id }),
                Categories = _service.GetCategories().Select(x => new DropDownViewModel {Id = x.Id, Description = x.Description }).OrderBy(x=>x.Description)
            };

DisplayTradersAttachedToCategoryViewModel

public class DisplayTradersAttachedToCategoryViewModel
{
    public int Id { get; set; }
    public string Description { get; set; }

    [UIHint("BusinessNameDisplayTemplate")]
    public IEnumerable<BusinessNameViewModel> BusinessName { get; set; }

    [UIHint("DropDownEditorTemplate")]
    public IEnumerable<DropDownViewModel> Categories { get; set; }
}

DropDownViewModel

public class DropDownViewModel
{
    public int Id { get; set; }
    public string Description { get; set; }
}

DropDownEditorTemplate

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Internal.ViewModels.Controller.DropDownViewModel>>" %>
<table class="aligncenter">
  <tr class="tRow left"><%
    if (Model != null)
    {%>
      <td>
        <%=Html.DropDownList("Category", Model.)%> 
      </td><%
    }%>                        
  </tr>
</table>

I dont know what I'm meant to provide within the DropDownList or if I'm actually doing it correct.

UPDATE

Controller

            var trader = new DisplayTradersAttachedToCategoryViewModel
            {
                Description = data.Description,
                Id = data.Id,
                BusinessName = traders.Select(x => new BusinessNameViewModel { BusinessName = x.BusinessName, Id = x.Id }),
                Categories = _service.GetCategories().Select(x => new SelectListItem() {Text = x.Description, Value = x.Id.ToString(), Selected = true})
            };

DisplayTradersAttachedToCategoryViewModel

    public int Id { get; set; }
    public string Description { get; set; }

    [UIHint("BusinessNameDisplayTemplate")]
    public IEnumerable<BusinessNameViewModel> BusinessName { get; set; }

    public IEnumerable<SelectListItem> Categories { get; set; }

    public int Category { get; set; }

    public string Button { get; set; } 

DeleteCategoryAttachedToTraders.aspx

using (Html.BeginForm("DeleteCategoryFromTradersAttachNewCategory", "Controller",FormMethod.Post))
    <%=Html.DropDownList("Categories")%>

Controller

[HttpPost]
            public ActionResult DeleteCategoryFromTradersAttachNewCategory(DisplayTradersAttachedToCategoryViewModel displayTradersAttachedToCategoryViewModel)
            {
                if (displayTradersAttachedToCategoryViewModel.Button == "Back to List") return RedirectToAction("ViewCategories");

            //Update traders with new category
            //delete category

            //if (_service.DeleteCategory((int)deleteCategoryViewModel.Id))
            //{
            //    return RedirectToAction("ViewCategories");
            //}

            return View("DeleteCategoryAttachedToTraders", displayTradersAttachedToCategoryViewModel);
        }

When I access the controller via the Submit button nothing is stored within the DisplayTradersAttachedToCategoryViewModel. What am I doing wrong?

Thanks in advance for any help.

Clare

3 Answers 3

2

Try like this:

 <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Tameside.Internal.ViewModels.BuyWithConfidence.DropDownViewModel>>" %>
 <select>
     <% foreach(var category in Model.Categories) { %>
         <option value="<%= Html.Encode(category.Id) %>"><%= Html.Encode(category.Name %></option>
     <% } %>
 </select>

ofcourse Id and Name are just members out of my head. Use those which are in your category class.

Sign up to request clarification or add additional context in comments.

3 Comments

How do I name the select list? At the moment the value is not being returned via the view model.
Just put the 'name' attribute to the select tag
It's still a good idea to mark my post as answer.. Thanks in advice ;)
0

you can try casting it to (IEnumerable<SelectListItem>) i.e.

<%=Html.DropDownList("Category", (IEnumerable<SelectListItem>)Model.Categories)%> 

-or-

create a model property of type SelectList and you can initialize it as

new SelectList(Categories.ToList(), "ID", "Description");

3 Comments

This cast will not work.. how can it cast Category to SelectListItem? Also... You give a whole object as a parameter to DropDownList helper and in my opinion it cannot recognize what should be display and what should be value member in html code that it generates.
try it out and see... also i had given alternate to it... instead of writing for each statement you can do it with SelectList
Hello ajay_whiz, I am trying it your way but can you have a look at my post again as the data isn't getting passed through the viewmodel
0

Thanks ŁukaszW.pl / ajay_whiz for your help :-) In the end I used a mixture of both answers:

Controller

Categories = _service.GetCategories(id).Select(x => new SelectListItem {Text = x.Description, Value = x.Id.ToString()}) 

DisplayTradersAttachedToCategoryViewModel

public IEnumerable<SelectListItem> Categories { get; set; }

DeleteCategoryAttachedToTraders.aspx

<%=Html.DropDownList("Category", Model.Categories)%> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.