在选择列表中设置默认值

发布于 2024-11-09 09:55:51 字数 2221 浏览 0 评论 0原文

我有以下类和编辑器模板,用于创建各种货币的下拉列表。

public class Currency
{
    public string CurrencyId { get; set; }
    public string CurrencyName { get; set; }
}

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<List<morkyc.Core.Model.Currency>>" %>

<tr>
    <td style="width:50%;">
    <label class="fieldLabel">
    Specify Currency :
    </label>
    </td>
    <td>
        <%= Html.DropDownListFor(model => model, new SelectList(Model, "CurrencyId", "CurrencyName", ))%>
     </td>
</tr>

我在控制器中创建了一个列表

 List<Currency> lCurrencyList = new List<Currency>(new Currency[]
        {
            new Currency{CurrencyId = "AED", CurrencyName = "United Arab Emirates Dirham (AED)"}, 
            new Currency{CurrencyId = "AFN", CurrencyName = "Afghan Afghani (AFN)"}, 
            new Currency{CurrencyId = "ALL", CurrencyName = "Albanian Lek (ALL)"}, 
            new Currency{CurrencyId = "AMD", CurrencyName = "Armenian Dram (AMD)"}, 
            new Currency{CurrencyId = "ANG", CurrencyName = "Netherlands Antillean Guilder (ANG)"},
            new Currency{CurrencyId = "AOA", CurrencyName = "Angolan Kwanza (AOA)"},
            new Currency{CurrencyId = "ARS", CurrencyName = "Argentine Peso (ARS)"}, 
            new Currency{CurrencyId = "AUD", CurrencyName = "Australian Dollar (AUD)"}, 
            new Currency{CurrencyId = "AWG", CurrencyName = "Aruban Florin (AWG)"}, 
            new Currency{CurrencyId = "AZN", CurrencyName = "Azerbaijani Manat (AZN)"}, 
            new Currency{CurrencyId = "BAM", CurrencyName = "Bosnia-Herzegovina Convertible Mark (BAM)"}, 
            new Currency{CurrencyId = "BBD", CurrencyName = "Barbados Dollar (BBD)"}, 
            new Currency{CurrencyId = "BDT", CurrencyName = "Bangladeshi Taka (BDT)"}, 
            new Currency{CurrencyId = "BGN", CurrencyName = "Bulgarian Lev (BGN)"},
new Currency{CurrencyId = "ZWD", CurrencyName = "Zimbabwe Dollar (ZWD)"}
        });

在我看来,我调用以下视图来创建下拉列表

<%= Html.EditorFor(model => model.Currency)%>

这工作得很好。

有人可以建议我如何设置默认选定的项目吗?

I have the following class and editor template for creating a dropdownlist for various currencies.

public class Currency
{
    public string CurrencyId { get; set; }
    public string CurrencyName { get; set; }
}

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<List<morkyc.Core.Model.Currency>>" %>

<tr>
    <td style="width:50%;">
    <label class="fieldLabel">
    Specify Currency :
    </label>
    </td>
    <td>
        <%= Html.DropDownListFor(model => model, new SelectList(Model, "CurrencyId", "CurrencyName", ))%>
     </td>
</tr>

I create a list in my controller

 List<Currency> lCurrencyList = new List<Currency>(new Currency[]
        {
            new Currency{CurrencyId = "AED", CurrencyName = "United Arab Emirates Dirham (AED)"}, 
            new Currency{CurrencyId = "AFN", CurrencyName = "Afghan Afghani (AFN)"}, 
            new Currency{CurrencyId = "ALL", CurrencyName = "Albanian Lek (ALL)"}, 
            new Currency{CurrencyId = "AMD", CurrencyName = "Armenian Dram (AMD)"}, 
            new Currency{CurrencyId = "ANG", CurrencyName = "Netherlands Antillean Guilder (ANG)"},
            new Currency{CurrencyId = "AOA", CurrencyName = "Angolan Kwanza (AOA)"},
            new Currency{CurrencyId = "ARS", CurrencyName = "Argentine Peso (ARS)"}, 
            new Currency{CurrencyId = "AUD", CurrencyName = "Australian Dollar (AUD)"}, 
            new Currency{CurrencyId = "AWG", CurrencyName = "Aruban Florin (AWG)"}, 
            new Currency{CurrencyId = "AZN", CurrencyName = "Azerbaijani Manat (AZN)"}, 
            new Currency{CurrencyId = "BAM", CurrencyName = "Bosnia-Herzegovina Convertible Mark (BAM)"}, 
            new Currency{CurrencyId = "BBD", CurrencyName = "Barbados Dollar (BBD)"}, 
            new Currency{CurrencyId = "BDT", CurrencyName = "Bangladeshi Taka (BDT)"}, 
            new Currency{CurrencyId = "BGN", CurrencyName = "Bulgarian Lev (BGN)"},
new Currency{CurrencyId = "ZWD", CurrencyName = "Zimbabwe Dollar (ZWD)"}
        });

In my view i call the following view to create the dropdownlist

<%= Html.EditorFor(model => model.Currency)%>

This is working perfectly fine.

Could anybody please suggest as to how can i set the default selected item ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蝶舞 2024-11-16 09:55:51

您的编辑器模板是强类型的货币列表。此外,您还将列表作为第一个参数传递给 DropDownListFor 帮助器,这不好。您永远不会传递某些选定的值,因此在此编辑器模板中您可以做的最好的事情就是将该值设置为此列表的第一个元素。

<%= Html.DropDownListFor(
    model => model, 
    new SelectList(Model, "CurrencyId", "CurrencyName", "AED")
)%>

但我猜你想动态传递这个值。所以我会稍微修改你的视图模型:

public class CurrencyViewModel
{
    public string SelectedCurrency { get; set; }
    public IEnumerable<SelectListItem> Currencies { get; set; }
}

然后有以下编辑器模板:

<%@ Control 
    Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<CurrencyViewModel>" %>

<tr>
    <td style="width:50%;">
        <label class="fieldLabel">
            Specify Currency :
        </label>
    </td>
    <td>
        <%= Html.DropDownListFor(
            model => model.SelectedCurrency, 
            new SelectList(Model.Currencies, "Value", "Text")
        )%>
     </td>
</tr>

现在在你的控制器中:

public ActionResult Foo()
{
    var model = new CurrencyViewModel
    {
        // Define the selected value here
        SelectedCurrency = "AED",
        Currencies = new[]
        {
            new SelectListItem { Value = "AED", Text = "United Arab Emirates Dirham (AED)" }, 
            new SelectListItem{ Value = "AFN", Text = "Afghan Afghani (AFN)"}, 
            ...
        }
    };
    return View(model);
}

最后在视图中调用自定义编辑器模板:

<%= Html.EditorForModel() %>

Your editor template is strongly typed to a currency list. Also you are passing a list as first argument to the DropDownListFor helper which is not good. You are never passing some selected value, so the best you could do in this editor template is to set the value to the first element of this list for example.

<%= Html.DropDownListFor(
    model => model, 
    new SelectList(Model, "CurrencyId", "CurrencyName", "AED")
)%>

But I guess that you want to dynamically pass this value. So I would modify your view model a little:

public class CurrencyViewModel
{
    public string SelectedCurrency { get; set; }
    public IEnumerable<SelectListItem> Currencies { get; set; }
}

then have the following editor template:

<%@ Control 
    Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<CurrencyViewModel>" %>

<tr>
    <td style="width:50%;">
        <label class="fieldLabel">
            Specify Currency :
        </label>
    </td>
    <td>
        <%= Html.DropDownListFor(
            model => model.SelectedCurrency, 
            new SelectList(Model.Currencies, "Value", "Text")
        )%>
     </td>
</tr>

and now in your controller:

public ActionResult Foo()
{
    var model = new CurrencyViewModel
    {
        // Define the selected value here
        SelectedCurrency = "AED",
        Currencies = new[]
        {
            new SelectListItem { Value = "AED", Text = "United Arab Emirates Dirham (AED)" }, 
            new SelectListItem{ Value = "AFN", Text = "Afghan Afghani (AFN)"}, 
            ...
        }
    };
    return View(model);
}

and finally in the view invoke the custom editor template:

<%= Html.EditorForModel() %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文