1

ASP.NET MVC プロジェクトにフォームがあり、次のような選択ドロップダウン リストがあります。

<select>
  <option value="ee5711b9-ec86-4378-a975-ae10a4ebedbc">Volvo Account</option>
  <option value="0dc0e9d8-2245-43de-81a9-5b94c19646fa">Saab Account</option>
  <option value="f9a05ef6-9ca6-4eeb-9e04-79726a62b38c">Mercedes Account</option>
  <option value="1c5c2e43-06d6-4b7d-916a-231be535a608">Audi Account</option>
</select>

プロジェクトの後のページでは、GUID 識別子を使用して何かを行う必要がありますが、最初にユーザーに確認を求める必要があります。明らかに、GUID は役に立たず、わかりやすい名前 (つまり、"Volvo Account") を表示したいと考えています。

しかし、FormCollection の値を掘り下げると、選択したオプションのテキストではなく値しか取得できません。これは設計目標を考えると理にかなっていますが、テキスト値を POST する方法はありますか?

いくつかの回避策を考えることができます (JavaScript で非表示フィールドを設定する、事後に GUID を使用してルックアップを実行し、これを入力したのと同じ方法など)、これを行うための本質的な方法はありますか?

4

3 に答える 3

2

これを行うための固有の方法はありません。JavaScript が実際に最適なオプションです。

非表示の入力の値を選択した OPTION の表示値に設定する「onchange」イベント ハンドラを SELECT に追加します。

于 2009-08-25T20:46:53.550 に答える
0

非表示フィールドの使用:

<select>
    <option value="ee5711b9-ec86-4378-a975-ae10a4ebedbc">Volvo Account</option>
    <option value="0dc0e9d8-2245-43de-81a9-5b94c19646fa">Saab Account</option>
    <option value="f9a05ef6-9ca6-4eeb-9e04-79726a62b38c">Mercedes Account</option>
    <option value="1c5c2e43-06d6-4b7d-916a-231be535a608">Audi Account</option>
</select>
...
<input type="hidden" name="Options[0].Key" value="ee5711b9-ec86-4378-a975-ae10a4ebedbc" />
<input type="hidden" name="Options[0].Value" value="Volvo Account" />
<input type="hidden" name="Options[1].Key" value="0dc0e9d8-2245-43de-81a9-5b94c19646fa" />
<input type="hidden" name="Options[1].Value" value="Saab Account" />
...

コントローラ:

public ActionResult SomeAction(..., IDictionary<string, string> options)
{
    string selectedValue = ...;
    string selectedText = options[selectedValue];
}  
于 2009-08-26T05:01:42.213 に答える
0

I usually handle Drop Downs / Select Boxes in MVC with a static property within a Collection class in Models.

Example:

using System.Collections.Generic;

namespace SomeProject.Models
{
    public class Collections
    {
        public static Dictionary<string, string> AccountType = new Dictionary<string, string>
        {
            { "", "" },
            { "ee5711b9-ec86-4378-a975-ae10a4ebedbc", "Volvo Account" },
            { "0dc0e9d8-2245-43de-81a9-5b94c19646fa", "Saab Account" },
            { "f9a05ef6-9ca6-4eeb-9e04-79726a62b38c", "Mercedes Account" },
            { "1c5c2e43-06d6-4b7d-916a-231be535a608", "Audi Account" }
        };
    }
}

Then I would render the DropDown in the View like:

<%= Html.DropDownList("AccountType", new SelectList(SomeProject.Models.Collections.AccountType, "Key", "Value", Convert.ToString(ViewData["AccountType"])), "", new { tabindex = "3", @class = "dropdown" }) %>

Now that Collection is available to your entire application. You can grab whatever user-friendly name you'd like via:

SomeProject.Models.Collections.AccountType[ViewData["AccountType"]]

You could even render the collection out to the View if you desire.

Hope that helps.

于 2009-08-26T05:17:01.760 に答える