0

ビューモデルを使用して SOAP Web サービスから値を取得してドロップダウン リストに表示する方法についてアドバイスが必要です。現在、サービス クラス ライブラリ プロジェクト (n 層アプリケーション) にあるサービス クラスからさまざまなドロップダウン リストのデータを受け取っています。

ドロップダウン リスト サービスのコードは、以下のコードと同様の形式に従います。

public IEnumerable<SelectListItem> getValuesAsSelectItems(string selected)
    {
        var items = new List<SelectListItem>();

        items.Add(new SelectListItem { Text = "Please Select", Value = string.Empty, Selected = (selected == string.Empty) });

        foreach (var value in this.getValues())
        {
            items.Add(new SelectListItem { Text = value.Value, Value = value.Value, Selected = (selected == value.Value) });
        }
        return new SelectList(items, "Value", "Text");
    }

このサービスからビューモデルに値を渡す方法が必要です。ドロップダウン リストごとに 7 つのコントローラーを作成しました。これらのコントローラーはすべて、アプリケーション全体で再利用できる部分ビューにリンクされます。ドロップダウン リストには、タイトル、国、州などが含まれます。

4

1 に答える 1

1

取ることができるアプローチは、ドロップダウン リストの値を独自のビューモデルに抽出することです。そう:

ステップ 1:ItemsViewModelドロップダウン リスト項目をカプセル化するビュー モデル ( ) を作成します。

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Models
{
    public class DropDownListItem
    {
        public string Text { get; set; }
        public string Value { get; set; }
    }

    public class ItemsViewModel
    {
        private readonly List<DropDownListItem> _items;

        // The selected item:
        public string SelectedItem { get; set; }

        // The items:
        public IEnumerable<SelectListItem> Items
        {
            get
            {
                var allItems = _items.Select(i => new SelectListItem 
                {
                    Value = i.Value,
                    Text = i.Text
                });
                return DefaultItem.Concat(allItems);
            }
        }

        // Default item (i.e. the "select" text) if none selected
        public IEnumerable<SelectListItem> DefaultItem
        {
            get
            {
                return Enumerable.Repeat(new SelectListItem
                {
                    Value = "-1",
                    Text = "Select an item"
                }, count: 1);
            }
        }

        public ItemsViewModel()
        {

        }

        // Constructor taking in items from service and selected item string:
        public ItemsViewModel(List<DropDownListItem> items, string selected)
        {
            _items = items;
            SelectedItem = selected;
        }
    }
}

手順 2:にバインドする Views フォルダーに部分ビューを作成しますItemsViewModel

@model Models.ItemsViewModel
@Html.DropDownListFor(m => m.SelectedItem, Model.Items)

ステップ 3:適切なコントローラー (HomeController など) に、サービスからデータをプルする子アクション、ビュー モデル、および部分ビューを一緒に配置します。

[ChildActionOnly]
public ActionResult DropDownList(string type, string selected)
{
    // If you need to call different services based on the type (e.g. Country), then pass through "type" and base the call on that

    var items = new ItemsViewModel(
        (from g in _service.getTitles() select new DropDownListItem { Text = g.Text, Value = g.Value }).ToList(), 
        selected);

    return PartialView("DropDownPartial", items);
}  

ステップ 4:このコード行を、ドロップダウン リストが必要なビューにドロップします。

@Html.Action("DropDownList", "Home", new { selected = "2", type = "country" })

selectedおよびtypeは、適切と思われる方法を決定する必要があり、オプションであることに注意してください。

うまくいけば、これはあなたにいくつかのインスピレーションを与えます.

于 2013-03-14T15:58:27.047 に答える