具有下拉列表的Asp.Net MVC和SelectListItem协助

时间:2011-01-28 22:25:45

标签: c# asp.net-mvc selectlistitem

我正在尝试构建一个Dropdownlist,但正在与Html.DropDownList渲染进行斗争。

我有一个班级:

public class AccountTransactionView
{
    public IEnumerable<SelectListItem> Accounts { get; set; }
    public int SelectedAccountId { get; set; }
}

这基本上是我现在的视图模型。帐户列表以及用于返回所选项目的属性。

在我的控制器中,我准备好了这样的数据:

public ActionResult AccountTransaction(AccountTransactionView model)
{
    List<AccountDto> accounts = Services.AccountServices.GetAccounts(false);

    AccountTransactionView v = new AccountTransactionView
    {
        Accounts = (from a in accounts
                    select new SelectListItem
                    {
                        Text = a.Description,
                        Value = a.AccountId.ToString(),
                        Selected = false
                    }),
    };

    return View(model);
}

现在出现问题:

然后我试图在我看来构建Drop:

<%=Html.DropDownList("SelectedAccountId", Model.Accounts) %>

我收到以下错误:

具有键'SelectedAccountId'的ViewData项的类型为'System.Int32',但必须是'IEnumerable'类型。

为什么要我退回整个项目清单?我只想要选中的值。我该怎么做?

2 个答案:

答案 0 :(得分:29)

您有一个视图模型,您的视图强类型=&gt;使用强类型助手:

<%= Html.DropDownListFor(
    x => x.SelectedAccountId, 
    new SelectList(Model.Accounts, "Value", "Text")
) %>

另请注意,我使用SelectList作为第二个参数。

在您的控制器操作中,您返回的是作为参数传递的视图模型,而不是您在正确设置了Accounts属性的操作中构建的视图模型,因此这可能会有问题。我已经清理了一下:

public ActionResult AccountTransaction()
{
    var accounts = Services.AccountServices.GetAccounts(false);
    var viewModel = new AccountTransactionView
    {
        Accounts = accounts.Select(a => new SelectListItem
        {
            Text = a.Description,
            Value = a.AccountId.ToString()
        })
    };
    return View(viewModel);
}

答案 1 :(得分:15)

第1步:您的模型类

public class RechargeMobileViewModel
    {
        public string CustomerFullName { get; set; }
        public string TelecomSubscriber { get; set; }
        public int TotalAmount { get; set; }
        public string MobileNumber { get; set; }
        public int Month { get; set; }
        public List<SelectListItem> getAllDaysList { get; set; }

        // Define the list which you have to show in Drop down List
        public List<SelectListItem> getAllWeekDaysList()
        {
            List<SelectListItem> myList = new List<SelectListItem>();
            var data = new[]{
                 new SelectListItem{ Value="1",Text="Monday"},
                 new SelectListItem{ Value="2",Text="Tuesday"},
                 new SelectListItem{ Value="3",Text="Wednesday"},
                 new SelectListItem{ Value="4",Text="Thrusday"},
                 new SelectListItem{ Value="5",Text="Friday"},
                 new SelectListItem{ Value="6",Text="Saturday"},
                 new SelectListItem{ Value="7",Text="Sunday"},
             };
            myList = data.ToList();
            return myList;
        }
}

步骤2:调用此方法填充控制器中的下拉菜单

namespace MvcVariousApplication.Controllers
    {
        public class HomeController : Controller
        {
            public ActionResult Index()
            {
              RechargeMobileViewModel objModel = new RechargeMobileViewModel();
                objModel.getAllDaysList = objModel.getAllWeekDaysList();  
                return View(objModel);
            }
    }
    }

步骤3:按如下所示填写下拉列表视图

 @model MvcVariousApplication.Models.RechargeMobileViewModel
    @{
        ViewBag.Title = "Contact";
    }
    @Html.LabelFor(model=> model.CustomerFullName)
    @Html.TextBoxFor(model => model.CustomerFullName)

    @Html.LabelFor(model => model.MobileNumber)
    @Html.TextBoxFor(model => model.MobileNumber)

    @Html.LabelFor(model => model.TelecomSubscriber)
    @Html.TextBoxFor(model => model.TelecomSubscriber)

    @Html.LabelFor(model => model.TotalAmount)
    @Html.TextBoxFor(model => model.TotalAmount)

    @Html.LabelFor(model => model.Month)
    @Html.DropDownListFor(model => model.Month, new SelectList(Model.getAllDaysList, "Value", "Text"), "-Select Day-")