Hello all,
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