0

こんにちは私はMVC3の初心者です。ビューに新しいドロップダウンボックスを作成しようとしていますが、「'System.Web.Mvc.HtmlHelper'には'DropDownListFor'の定義が含まれておらず、最適な拡張メソッドのオーバーロード'System.Web」というエラーが表示されます。 Mvc.Html.SelectExtensions.DropDownListFor(System.Web.Mvc.HtmlHelper、System.Linq.Expressions.Expression>、System.Collections.Generic.IEnumerable)'に無効な引数がいくつかあります。

これがビューコードです

<tr>
    <td>
        <label>
        Customer Name
        </label>
    </td>
    <td>
   @Html.DropDownListFor(A => A.Roles, Model.Roles);
    </td>
</tr>

コントローラコード

 public ActionResult Index()
        {
            var Model = new Customer();
            Model.Roles = getRoles();

            return View(Model);
        }

        private List<string> getRoles()
        {
            List<string> roles = new List<string> 
            {
                "Developer",
                "Tester",
                "Project Manager",
                "Team Lead",
                "QA"
            };
            return roles;
        }
4

1 に答える 1

0

まず、ビューのビューモデル クラスを作成することをお勧めします。

public class IndexViewModel
{
    public IList<string> Roles { get; set; }

    public string SelectedRole { get; set; }
}

次に、次のようにビューを呼び出します。

public ActionResult Index()
{
    List<string> roles = new List<string> 
    {
        "Developer",
        "Tester",
        "Project Manager",
        "Team Lead",
        "QA"
    };

    var viewModel = new IndexViewModel();

    viewModel.Roles = roles;

    return this.View(viewModel);
}

最後に、ドロップダウン リストをレンダリングします。

@model Mvc4.Controllers.IndexViewModel

@Html.DropDownListFor(model => model.SelectedRole, new SelectList(Model.Roles))

ドロップダウン ヘルパーは 2 番目のパラメーターに を使用できないため、選択したアイテム ( SelectedRole) を格納するための変数が必要であり、ロールの選択を にラップする必要があります。SelectListIEnumerable

于 2013-01-12T16:12:15.200 に答える