Asp.Net MVC的下拉列表,SelectListItem援助 [英] Asp.Net MVC with Drop Down List, and SelectListItem Assistance

查看:122
本文介绍了Asp.Net MVC的下拉列表,SelectListItem援助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立一个DropDownList,但与Html.DropDownList渲染作战。

我有一个类:

 公共类AccountTransactionView
{
    公共IEnumerable的< SelectListItem>帐户{搞定;组; }
    公众诠释SelectedAccountId {搞定;组; }
}

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

在我的控制器,我准备像这样的数据:

 公众的ActionResult AccountTransaction(AccountTransactionView模型)
{
    清单< AccountDto>账户= Services.AccountServices.GetAccounts(假);    AccountTransactionView V =新AccountTransactionView
    {
        账户=(从账户中
                    选择新SelectListItem
                    {
                        文= a.Description,
                        值= a.AccountId.ToString(),
                        选择= FALSE
                    }),
    };    返回查看(模型);
}

现在的问题:

我再试图建立跌落在我看来下来:

 <%= Html.DropDownList(SelectedAccountId,Model.Accounts)%GT;

我收到以下错误:

这有钥匙'SelectedAccountId的类型为System.Int32,但必须是类型为IEnumerable的。该项目的ViewData

它为什么要我回去项目的整个名单?我只是想选择的值。我应该怎么做呢?


解决方案

您必须到您的看法是强类型的视图模型=>使用强类型的辅助方法:

 <%= Html.DropDownListFor(
    X => x.SelectedAccountId,
    新的SelectList(Model.Accounts,值,文本)
)%GT;

另请注意,我用的是的SelectList 的第二个参数。

而在你的控制器动作,你正在返回作为参数传递的视图模型,而不是你哪个有正确的帐户属性设置所以这可能是有问题的行动里面构建的之一。我已经洗干净了一下:

 公众的ActionResult AccountTransaction()
{
    VAR账户= Services.AccountServices.GetAccounts(假);
    VAR视图模型=新AccountTransactionView
    {
        账户= accounts.Select(A =>新建SelectListItem
        {
            文= a.Description,
            值= a.AccountId.ToString()
        })
    };
    返回视图(视图模型);
}

I am trying to build a Dropdownlist, but battling with the Html.DropDownList rendering.

I have a class:

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

That is basically my view model for now. The list of Accounts, and a property for returning the selected item.

In my controller, I get the data ready like this:

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);
}

Now the problem:

I am then trying to build the Drop down in my view:

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

I am getting the following error:

The ViewData item that has the key 'SelectedAccountId' is of type 'System.Int32' but must be of type 'IEnumerable'.

Why would it want me to return the whole list of items? I just want the selected value. How should I be doing this?

解决方案

You have a view model to which your view is strongly typed => use strongly typed helpers:

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

Also notice that I use a SelectList for the second argument.

And in your controller action you were returning the view model passed as argument and not the one you constructed inside the action which had the Accounts property correctly setup so this could be problematic. I've cleaned it a bit:

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);
}

这篇关于Asp.Net MVC的下拉列表,SelectListItem援助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆