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.Cont开发者_如何学Croller.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
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.
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");
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)%>
精彩评论